Inserting Page List into form->select array

Permalink
Hello!

I'm pulling a page list into a custom block edit.php in order to populate values in a dropdown list in a select field I'm generating from the form helper.

Ultimately, in the array of pages, I expect that the page collection ID would be the value and the page collection name would be the option text.

So THIS is what I'd like to achieve...

<select name="my_select_field" id="my_select_field" class="ccm-input-select">
<option value="123">My Awesome Page</option>
<option value="542">My Other Awesome Page</option>
<option value="983">Only Sort of Awesome Page</option>
</select>


Only, that's not what I'm getting at all. It seems no matter what I've tried in the array to get the page ID to be the option value, it gets ignored.

So here is the code I'm using to grab the page list and create the array.

<?php
Loader::model('page_list');
$pl = new PageList();
// I'm specifying a parent page to look under / filter by here
$cParentID = '503';
$pl->filterByParentID($cParentID);
$pages = $pl->getPage();
$venues = array();
foreach ($pages as $page) :
  $venues[$page->getCollectionID()] = $page->getCollectionName();
endforeach;
// I'd like to put these into the array
// I don't think that's a problem right?
array_unshift($venues, "-- Choose One --");
array_push($venues, "To Be Determined");


My select form code looks like:
<?php 
echo $form->select('my_select_field', $venues, $my_select_field);
?>


As I said, I'm getting regular 0,1,2,3,etc as my option values instead of the page ID like I expected. So the output looks like this:

<select name="my_select_field" id="my_select_field" class="ccm-input-select">
<option value="0">My Awesome Page</option>
<option value="1">My Other Awesome Page</option>
<option value="2">Only Sort of Awesome Page</option>
</select>


I tried to dump the page ids into one array and then the titles into another and then do an array_combine but had zero luck with that too.

Thanks in advance for any help! :D

SpatialAnomaly
 
exchangecore replied on at Permalink Reply
exchangecore
Hmmm.. Maybe the array unshift is nabbing you? Not sure if PHP *thinks* that those collection ID's are a numeric key or not but from the PHP docs:

http://www.php.net/manual/en/function.array-unshift.php...

array_unshift() prepends passed elements to the front of the array. Note that the list of elements is prepended as a whole, so that the prepended elements stay in the same order. All numerical array keys will be modified to start counting from zero while literal keys won't be touched.
JohntheFish replied on at Permalink Reply
JohntheFish
You can insert the variable dump or logging code in this howto to see just where $venues is going astray:

http://www.concrete5.org/documentation/how-tos/developers/concrete5...

I expect push does the same as unshift, re-indexing numerically.
(EDIT - for some reason I missed @exchangecore's answer until after I had posted, so have removed duplication)

Try:
$venues = array("-- Choose One --");
foreach ($pages as $page){
  $venues[$page->getCollectionID()] = $page->getCollectionName();
}
$venues[] = "To Be Determined";