function _hierarchical_select_options in Hierarchical Select 6.3
Same name and namespace in other branches
- 5.3 hierarchical_select.module \_hierarchical_select_options()
- 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. TODO: rename to _hierarchical_select_select_options().
1 call to _hierarchical_select_options()
- theme_hierarchical_select_select in includes/
theme.inc - Format a select in the .hierarchial-select div: prevent it from being wrapped in a div. This simplifies the CSS and JS code.
File
- includes/
theme.inc, line 404 - All theme functions for the Hierarchical Select module.
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;
}