form_select_options
- Versions
- 4.7 – 7
form_select_options($element, $choices = NULL)
Convert a select form element's options array into an HTML.
Parameters
$element An associative array containing the properties of the element.
$choices Mixed: Either an associative array of items to list as choices, or an object with an 'option' member that is an associative array. This parameter is only used internally and should not be passed.
Return value
An HTML string of options for the select form element.
Related topics
Code
includes/form.inc, line 1663
<?php
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 = is_array($element['#value']);
$options = '';
foreach ($choices as $key => $choice) {
if (is_array($choice)) {
$options .= '<optgroup label="' . $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;
}
?>Login or register to post comments 