Same name and namespace in other branches
  1. 10 core/includes/form.inc \form_select_options()
  2. 4.7.x includes/form.inc \form_select_options()
  3. 6.x includes/form.inc \form_select_options()
  4. 7.x includes/form.inc \form_select_options()
  5. 8.9.x core/includes/form.inc \form_select_options()
  6. 9 core/includes/form.inc \form_select_options()

Related topics

1 call to form_select_options()
theme_select in includes/form.inc
Format a dropdown menu or scrolling selection box.

File

includes/form.inc, line 940

Code

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;
}