Problem
If you use form select options with optgroups, the options attributes are never added to the options.
Steps to Reproduce
Use a select element similar to this example:
$form['states'] = array(
'#type' => 'select',
'#title' => t('States'),
'#options' => array(
'A' => array(
'AL' => t('Alabama'),
'AK' => t('Alaska'),
'AZ' => t('Arizona'),
'AR' => t('Arkansas'),
),
// ...
'W' => array(
'WI' => t('Wisconsin'),
'WY' => t('Wyoming'),
),
),
'#options_attributes' => array(
'AL' => array('class' => array('southeast'), 'data-bbq-meat' => 'pork')),
'AK' => array('class' => array('non-contiguous'), 'data-bbq-meat' => 'salmon')),
'AZ' => array('class' => array('southwest'), 'data-bbq-meat' => 'rattlesnake')),
'AR' => array('class' => array('south'), 'data-bbq-meat' => 'beef')),
// ...
'WI' => array('class' => array('midwest'), 'data-bbq-meat' => 'cheese')),
'WY' => array('class' => array('flyover'), 'data-bbq-meat' => 'bison')),
),
'#attributes' => array('class' => array('states-bbq-selector')),
);
Comments
Comment #2
dbouman commentedHere is a patch that will at least support adding attributes to elements using optgroups.
You still need to have the option attributes array without group keys.
Comment #3
zengenuity commentedComment #6
zengenuity commentedDespite the error above, the patch is fine. I added a test for this before applying the patch, and it screwed up testbot.
Thanks for patch. Committed.
Comment #8
ezoulou commentedFYI : the patch from #2 is actually included in the latest release of the Form options attributes module.
Your code should be formatted like the original message :