Converts an array of options into HTML, for use in select list form elements.
This function calls itself recursively to obtain the values for each optgroup within the list of options and when the function encounters an object with an 'options' property inside $element['#options'].
array $element: An associative array containing the following key-value pairs:
array|null $choices: (optional) Either an associative array of options in the same format as $element['#options'] above, or NULL. This parameter is only used internally and is not intended to be passed in to the initial function call.
string An HTML string of options and optgroups for use in a select form element.
function form_select_options($element, $choices = NULL) {
if (!isset($choices)) {
$choices = $element['#options'];
}
// array_key_exists() accommodates the rare event where $element['#value'] is NULL.
// isset() fails in this situation.
$value_valid = isset($element['#value']) || array_key_exists('#value', $element);
$value_is_array = $value_valid && is_array($element['#value']);
$options = '';
foreach ($choices as $key => $choice) {
if (is_array($choice)) {
$options .= '<optgroup label="' . check_plain($key) . '">';
$options .= form_select_options($element, $choice);
$options .= '</optgroup>';
}
elseif (is_object($choice)) {
$options .= form_select_options($element, $choice->option);
}
else {
$key = (string) $key;
if ($value_valid && (!$value_is_array && (string) $element['#value'] === $key || $value_is_array && in_array($key, $element['#value']))) {
$selected = ' selected="selected"';
}
else {
$selected = '';
}
$options .= '<option value="' . check_plain($key) . '"' . $selected . '>' . check_plain($choice) . '</option>';
}
}
return $options;
}