You are here

function form_select_options in Drupal 4

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

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>';
    }
    else {
      $key = (string) $key;
      if ($value_valid && ($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;
}