You are here

function _hierarchical_select_hierarchy_enforce_deepest in Hierarchical Select 6.3

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

Helper function to update the lineage of the hierarchy to ensure that the user selects an item in the deepest level of the hierarchy.

Parameters

$lineage: The lineage up to the deepest selection the user has made so far.

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

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

Return value

The updated lineage.

1 call to _hierarchical_select_hierarchy_enforce_deepest()
_hierarchical_select_hierarchy_generate in ./hierarchical_select.module
Generate the hierarchy object.

File

./hierarchical_select.module, line 2057
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_enforce_deepest($lineage, $module, $params) {

  // Use the deepest item as the first parent. Then apply this algorithm:
  // 1) get the parent's children, stop if no children
  // 2) choose the first child as the option that is selected by default, by
  //    adding it to the lineage of the hierarchy
  // 3) make this child the parent, go to step 1.
  $parent = end($lineage);

  // The last item in the lineage is the deepest one.
  $children = module_invoke($module, 'hierarchical_select_children', $parent, $params);
  while (count($children)) {
    $first_child = reset(array_keys($children));
    $lineage[] = $first_child;
    $parent = $first_child;
    $children = module_invoke($module, 'hierarchical_select_children', $parent, $params);
  }
  return $lineage;
}