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

dbouman created an issue. See original summary.

dbouman’s picture

Here 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.

zengenuity’s picture

Status: Active » Needs review

  • zengenuity committed 13ae59e on 7.x-1.x authored by dbouman
    Issue #2549767 by dbouman: Attributes are ignored if you use optgroup...

Status: Needs review » Needs work

The last submitted patch, 2: 2549767-add-support-for-attributes-with-optgroups.patch, failed testing.

zengenuity’s picture

Status: Needs work » Fixed

Despite 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.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.

ezoulou’s picture

FYI : 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 :

$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')),
);