You are here

function _hierarchical_select_hierarchy_validate in Hierarchical Select 7.3

Same name and namespace in other branches
  1. 5.3 hierarchical_select.module \_hierarchical_select_hierarchy_validate()
  2. 6.3 hierarchical_select.module \_hierarchical_select_hierarchy_validate()

Reset the selection if no valid item was selected. The first item in the array corresponds to the first selected term. As soon as an invalid item is encountered, the lineage from that level to the deeper levels should be unset. This is so to ignore selection of a level label.

Parameters

$selection: Either a single item id or an array of item ids.

$module: The module that should be used for HS hooks.

$params: The module that should be passed to HS hooks.

Return value

The updated selection.

3 calls to _hierarchical_select_hierarchy_validate()
_hierarchical_select_hierarchy_generate in ./hierarchical_select.module
Generate the hierarchy object.
_hierarchical_select_process_calculate_return_value in ./hierarchical_select.module
Calculate the return value of a hierarchical_select form element, based on the $hierarchy and $dropbox objects. We have to set a return value, because the values set and used by this form element ($element['#value]) are not easily usable in the…
_hierarchical_select_process_get_hs_selection in ./hierarchical_select.module
Get the current (flat) selection of the hierarchical select.

File

./hierarchical_select.module, line 2066
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_hierarchy_validate($selection, $module, $params) {
  $valid = TRUE;
  $selection_levels = count($selection);
  for ($i = 0; $i < $selection_levels; $i++) {

    // As soon as one invalid item has been found, we'll stop validating; all
    // subsequently selected items will be removed from the selection.
    if ($valid) {
      $valid = module_invoke($module, 'hierarchical_select_valid_item', $selection[$i], $params);
      if ($i > 0) {
        $parent = $selection[$i - 1];
        $child = $selection[$i];
        $children = array_keys(module_invoke($module, 'hierarchical_select_children', $parent, $params));
        $valid = $valid && in_array($child, $children);
      }
    }
    if (!$valid) {
      unset($selection[$i]);
    }
  }
  if (empty($selection)) {
    $selection = -1;
  }
  if (is_array($selection)) {

    // This is needed because we may have unset some values and we don't want
    // any gaps in the indexes (ie. the indexes would be 0,1,3 if we did
    // "$selection[] = X" after unsetting #2).
    $selection = array_values($selection);
  }
  return $selection;
}