You are here

function form_select_options in Drupal 6

Same name and namespace in other branches
  1. 8 core/includes/form.inc \form_select_options()
  2. 4 includes/form.inc \form_select_options()
  3. 5 includes/form.inc \form_select_options()
  4. 7 includes/form.inc \form_select_options()
  5. 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 1515

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="' . 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;
}