When you create a select box using optgroups, e.g grouping options by a bold label within a select box, the default value always ends up in 'other' even though it's a valid list value.
e.g:
<?php
// Make an optgroup style options array, grouped by vocabulary name
foreach($result as $row){
$options[$row->vocabulary_name][$row->tid] = $row->term_name;
}
?>
Just to illustrate, the #options array ends up looking something like this:
Array
(
[] => None
[Opinion] => Array
(
[19] => Blogs
[18] => Columns
[20] => Debates
[17] => Insight
[22] => Letters
[21] => Polls
)
[Lifestyle] => Array
(
[23] => Arts
[24] => Beauty
[25] => Books
[26] => Education
[27] => Entertainment
[29] => Family
[28] => Fashion
[31] => Film
[30] => Food
[32] => Health
[33] => Interiors
[35] => Motoring
[34] => Music
[36] => Technology
[37] => Travel
)
...
I suspect it's to do with select_or_other_element_process
and the check
if ($val
&& isset($element['select']['#options'])
&& is_array($element['select']['#options'])
&& !array_key_exists($val, $element['select']['#options'])
&& !in_array($val, $element['select']['#options'])) {
which may need to recurse into the #options array to find the value within the optgroup.
Curious if easy fix, else will need to workaround, possibly by overriding the _process function which would rather not do if patch available.
Lastly, thanks for the useful module!
DT
Comment | File | Size | Author |
---|---|---|---|
#2 | 1236360-select_or_other.module.optgroup-default-value-fix.patch | 1.32 KB | davidwhthomas |
#1 | select_or_other.module.optgroup-default-value-fix.patch | 1.34 KB | davidwhthomas |
Comments
Comment #1
davidwhthomas CreditAttribution: davidwhthomas commentedOK, some additional debugging showed the above hunch to be correct.
The attached patch fixes the issue, to allow default value to be correctly set when using both types of Drupal select boxes
It replaces
array_key_exists
with a simple_multi_array_key_exists
function in the default value check.Tested and working, please commit when possible.
Thanks again,
DT
Comment #2
davidwhthomas CreditAttribution: davidwhthomas commentedAdjusted patch as per Drupal coding guidelines.
Comment #2.0
davidwhthomas CreditAttribution: davidwhthomas commentedAdd sample optgroup #options array
Comment #3
danielb CreditAttribution: danielb commentedlol still not even slightly close to Drupal guidelines, but I can take care of that, thanks
I agree with your solution.
I can see this is not the only module that needed such a function: http://drupalcode.org/project/account_profile.git/blob/bf367e4196547d66c...
You can see it at the bottom of the page.
Comment #4
davidwhthomas CreditAttribution: davidwhthomas commentedYeah, I got that function from php.net, not drupalized ;)
I was thinking, may be better to prefix / namespace function as
select_or_other_multi_array_key_exists
, to prevent collision.Thanks for looking into it.
DT
Comment #5
danielb CreditAttribution: danielb commentedI have filed an issue with them too #1236468: potential function name conflict: _multi_array_key_exists()
Comment #6
danielb CreditAttribution: danielb commentedI've committed this now. Thanks.
Comment #7.0
(not verified) CreditAttribution: commentedFormatting