You are here

function _hierarchical_select_validate_selection in Hierarchical Select 5.2

Reset the selection if no valid item was selected. If an array is passed (this happens when the "save lineage" option is enabled), then 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.

1 call to _hierarchical_select_validate_selection()
hierarchical_select_get_hierarchy in ./hierarchical_select.module
Generate the hierarchy object.

File

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

Code

function _hierarchical_select_validate_selection($selection, $module, $params) {

  // Reset if no item was selected or the item's id could not be validated.
  $selection = empty($selection) ? -1 : $selection;
  if (is_array($selection)) {

    // The "save lineage" option is enabled because $selection is an array.
    $valid = TRUE;
    for ($i = 0; $i < count($selection); $i++) {
      if ($valid) {
        $valid = module_invoke($module, 'hierarchical_select_valid_item', $selection[$i], $params);
      }
      if (!$valid) {
        unset($selection[$i]);
      }
    }
    if (empty($selection)) {
      $selection = -1;
    }
  }
  elseif ($selection != 'none' && !module_invoke($module, 'hierarchical_select_valid_item', $selection, $params)) {
    $selection = -1;
  }
  return $selection;
}