I’ve been experiencing a problem with creating a select with multi-array. It creates nicely “optgroup” but the ‘default_value’ seams to act not like I’ve hoped.
The question I have is: should I add this as a patch or do I violate another multi-array issue?

Here is a detailed explanation of the problem. If I give an array like:

array(peronal => array(first, test, test2), default => array(private, public, current));

And would like to set a default value as:

array(default => 1)

I would expect:

<optgroup label="personal">
 <option value="0">first</option>
 <option value="1">test</option>
 <option value="2">test2</option>
</optgroup>
<optgroup label="default">
 <option value="0">private</option>
 <option value="1" selected="selected">public</option>
 <option value="2">current</option>
</optgroup>

Bot get:

<optgroup label="personal">
 <option value="0">first</option>
 <option value="1" selected="selected">test</option>
</optgroup>
<optgroup label="default">
 <option value="0">private</option>
 <option value="1" selected="selected">public</option>
 <option value="2">current</option>
</optgroup>

Notice both ‘test” as ‘public’ is selected.

When you go to the “form_select_options” function in “from.inc”, you’ll see how the recursion is handled, I’ve played around to make the recursion work like I wanted, only needed to change:
$options .= form_select_options($element, $choice);
to:
if($value_is_array) {
 $rec_el = $element;
 $rec_el['#value'] = $element['#value'][$key];
 $options .= form_select_options($rec_el, $choice);
} else {
 $options .= form_select_options($element, $choice);
}

Comments

chop’s picture

I too have experienced problems using nested arrays of options in order to create OPTGROUPings, in that you cannot properly set a '#default_value' and the formAPI doesn't seem able to save state after submission. Fixing this problem would be great. I've abandoned the use of OPTGROUPs alltogether because it doesn't work right now in Drupal 5.x.

This is an important accessibility and usability issue. Has it been addressed in Drupal 6.x or 7.x yet?

Christopher
www.ourbrisbane.com

queenielow’s picture

I think this may help.

I found this solution which i was looking for a long time.

$form['filter']['category']=array(
'#type' => 'select',
'#options'=>array('%'=>'Any','Categories'=>array('1'=>List1)),
'#title'=>t('Category Name'),
);

I works fine for me.. the 0 will be replaced with Categories.

mgifford’s picture

Issue tags: +Accessibility

Adding accessibility tag and links to best practice - http://www.w3.org/TR/WCAG20-GENERAL/H85.html

Everett Zufelt’s picture

I took a look at http://webaxe.blogspot.com/2007/07/podcast-51-option-groups-optgroup.html and tested the example select list with the most recent version of the JAWS screenreader (10.0.1154) and Firefox 3.5.

The screen-reader completely ignores the optgroup items in the list.

As much as this is an accessibility issue I cannot imagine a solution that can be provided by Drupal, this issue needs to be addressed by assistive technology providers. If Comment #2 properly addresses the original concern in the issue description I see no reason for this issue not to be set to "won't fix".

Everett Zufelt’s picture

Status: Active » Closed (won't fix)

This seems to be more of a support issue than a bug and the original poster hasn't been back for over a year. And as there is no way to correct any accessibility concern I have marked this as won't fix.