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

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

davidwhthomas’s picture

Status: Active » Needs review
FileSize
1.34 KB

OK, 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

davidwhthomas’s picture

Adjusted patch as per Drupal coding guidelines.

davidwhthomas’s picture

Issue summary: View changes

Add sample optgroup #options array

danielb’s picture

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

davidwhthomas’s picture

Yeah, 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

danielb’s picture

danielb’s picture

Status: Needs review » Fixed

I've committed this now. Thanks.

Status: Fixed » Closed (fixed)

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

Anonymous’s picture

Issue summary: View changes

Formatting

  • Commit d16ac2e on 7.x-2.x, 7.x-3.x, 8.x-3.x by danielb:
    Issue #1236360 by davidwhthomas, danielb: Default value with optgroups...