Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
Problem/Motivation
Form controls of type select
will group options in HTML <optgroup> tags if the #options
array contains a sub-array (the key for the sub-array will be used as the optgroup label). For example, in D7, the following code will output the control seen in the attached Screen Shot 2014-06-10 at 11.24.01.png
:
/**
* Form constructor for the select control with option-groups example.
*
* @see user_login_form_validate()
* @see user_login_form_submit()
*
* @ingroup forms
*/
function form_example_select_optgroup($form, &$form_state) {
$form['test_select'] = array(
'#type' => 'select',
'#title' => t('Selected'),
'#options' => array(
'1' => t('One'),
t('Two') => array(
'2.1' => t('Two point one'),
'2.2' => t('Two point two'),
),
'3' => t('Three'),
),
);
}
... however, this behaviour is not documented in the Form API Reference for the select control nor the Form API Reference for the #options property.
Proposed resolution
Add documentation to the select control and the options property in the Form API Reference.
Remaining tasks
- Write a patch for 8.x
- Review and RTBC
- Commit to 8.x
- Write a patch for 7.x
- Review and RTBC
- Commit to 7.x
User interface changes
None.
API changes
None.
Comment | File | Size | Author |
---|---|---|---|
#7 | interdiff-5-7.txt | 894 bytes | Anonymous (not verified) |
#7 | document_how_optgroups-2283703-7.patch | 1.43 KB | Anonymous (not verified) |
#5 | interdiff-3-5.txt | 855 bytes | Anonymous (not verified) |
#5 | document_how_optgroups-2283703-5.patch | 1.45 KB | Anonymous (not verified) |
#3 | document_how_optgroups-2283703-3.patch | 1.03 KB | Anonymous (not verified) |
Comments
Comment #1
jhodgdonForm API reference is in the Documentation project git repository, and is also nearly impossible to edit. Good luck.
Comment #2
jhodgdonFor Drupal 8, we need to document this on the Select render element, which is where we are documenting form/render elements now instead of in the Form API reference.
Comment #3
Anonymous (not verified) CreditAttribution: Anonymous at XIO commentedHow about this?
Comment #4
jhodgdonLooks good! Can we add a usage example for optgroups too?
Comment #5
Anonymous (not verified) CreditAttribution: Anonymous at XIO commentedYes, we can.
In the example I cast the TranslatableMarkup object so it can be used as a key, and that works, but it's possible this is not the right thing to do here?
Comment #6
jhodgdonGreat, thanks! A bit of tweaking...
In the other Element classes, we used
Usage example:
not
Example usage:
as the header. Let's stay consistent.
How about if we just use '2' as the header here, rather than casting the t() output?
Comment #7
Anonymous (not verified) CreditAttribution: Anonymous at XIO commentedFixed all three items. Thanks for reviewing!
Comment #8
jhodgdonLooks good, thanks!
Comment #9
alexpottCommitted df03a9d and pushed to 8.0.x and 8.1.x. Thanks!