Index: includes/form.inc =================================================================== RCS file: /cvs/drupal/drupal/includes/form.inc,v retrieving revision 1.168 diff -u -p -r1.168 form.inc --- includes/form.inc 29 Dec 2006 00:19:58 -0000 1.168 +++ includes/form.inc 29 Dec 2006 20:18:45 -0000 @@ -942,24 +942,41 @@ function form_select_options($element, $ } /** - * Traverses a select element's #option array looking for the object that - * holds the given key. Returns FALSE if not found. As usual with functions - * that can return 0 or FALSE do not forget to use === and !== if needed. + * Traverses a select element's #option array looking for any values + * that hold the given key. Returns an array of indexes that match. + * + * This helper does not support optgroups (when the elements of the + * #options array are themselves arrays), and will return FALSE if + * arrays are found. The caller must either flatten/restore or + * manually do their manipulations in this case, since returning the + * index is not sufficient, and supporting this would make the "helper" + * too complicated and cumbersome to be of any help. + * + * As usual with functions that can return array() or FALSE, do not + * forget to use === and !== if needed. * * @param $element - * The select element. + * The select element to search. * @param $key * The key to look for. * @return - * The index of the object that held the $key with some value, or FALSE. + * Any array of indexes that match the given $key. Array will be + * empty if no elements were found. FALSE if optgroups were found. */ function form_get_option_key($element, $key) { - foreach ($element['#options'] as $index => $object) { - if (isset($object->option[$key])) { - return $index; + $keys = array(); + foreach ($element['#options'] as $index => $choice) { + if (is_array($choice)) { + return FALSE; + } + else if (is_object($choice) && isset($choice->option[$key])) { + $keys[] = $index; + } + else if ($index == $key) { + $keys[] = $index; } } - return FALSE; + return $keys; } /**