You are here

function _hierarchical_select_options in Hierarchical Select 5.3

Same name and namespace in other branches
  1. 6.3 includes/theme.inc \_hierarchical_select_options()
  2. 7.3 includes/theme.inc \_hierarchical_select_options()

This is an altered clone of form_select_options(). The reason: I need to be able to set a class on an option element if it contains a level label, to allow for level label styles. Secondly, I need to be able to mark childless items (i.e. "option" elements).

1 call to _hierarchical_select_options()
theme_hierarchical_select_select in ./hierarchical_select.module
Format a select in the .hierarchial-select div: prevent it from being wrapped in a div. This simplifies the CSS and JS code.

File

./hierarchical_select.module, line 2146
This module defines the "hierarchical_select" form element, which is a greatly enhanced way for letting the user select items in a hierarchy.

Code

function _hierarchical_select_options($element) {
  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) {
    $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 = '';
    }

    // If an option DOES NOT have child info, then it's a special option:
    // - label_\d+ (level label)
    // - none ("<none>")
    // - create_new_item ("<create new item>")
    // Only when it's a level label, we have to add a class to this option.
    if (!isset($element['#childinfo'][$key])) {
      $class = preg_match('/label_\\d+/', $key) ? ' level-label' : '';
    }
    else {
      $class = $element['#childinfo'][$key] == 0 ? 'has-no-children' : 'has-children';
    }
    $options .= '<option value="' . check_plain($key) . '" class="' . $class . '"' . $selected . '>' . check_plain($choice) . '</option>';
  }
  return $options;
}