I am writing a simple form module using the Form API (D7).
A nested select element renders array indexes above the main categories.
Here are relevant code snippets:
// Array of menu options.
$reg_options = array(
array(
t('Help') => array(
'GENERAL' => t('General Feedback'),
'WEBSITE' => t('The Web Site'),
),
),
... // followed by several similar to the above and a closing );
// The select element.
$form['question'] = array(
'#title' => t('My message concerns:'),
'#type' => 'select',
'#options' => $reg_options,
'#multiple' => FALSE,
);
I tried making labels such as 'Help' rather than t('Help'), but the indexes still dispaly. Could not find any similar questions or examples. Does the form need to be displayed via block? If so, what's the best way to go about that in Drupal 7?
Comments
It would help if you posted
It would help if you posted the generated html for the select.
It doesn't work that way.
It doesn't work that way.
#optionsmust be a flat array.You can create something like this, though:
<?php$reg_options = array(
'HELP' => t('Help'),
'GENERAL' => '--' . t('General Feedback'),
'WEBSITE' => '--' . t('The Web Site'),
),
?>
Creating a real hierarchical select element is much harder; there are several modules for that and related stuff.
Actually, the code for
Actually, the code for generating select options will handle a nested array, it treats the outer element as optgroups and the inner arrays as the options.
Indeed. My bad, I'm still
Indeed. My bad, I'm still living in past. :D
This:
<?php$reg_options = array(
t('Help') => array(
'GENERAL' => t('General Feedback'),
'WEBSITE' => t('The Web Site'),
),
);
?>
...does work, so there seems to be one level of nesting too much in the original code.
Issue Closed & Thanks
Yes, the array outside of the 'Help' level items was not needed. I based this on an example on p. 281 of Tomlinson & VanDyke's Pro Dupal 7 Development 3rd edition, Apress. I didn't need anything fancier than this, so I didn't try any select modules, but I did make a mental note for future projects. I have a screen shot of how this looked, but failed to find a way to upload it or create an attachment.
Thanks for the help on this. This post may help anyone else referring to that book on Form API.
Senior Web Developer
American Institute of Physics