You are here

function _hierarchial_select_enforce_deepest_selection in Hierarchical Select 5

Same name and namespace in other branches
  1. 5.2 hierarchical_select.module \_hierarchial_select_enforce_deepest_selection()

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.

$root_level: The options in the root level.

$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 _hierarchial_select_enforce_deepest_selection()
_hierarchical_select_render in ./hierarchical_select.module
Render the hierarchical select.

File

./hierarchical_select.module, line 241
This module defines the "hierarchical_select" form element, which is a greatly enhanced way for letting the user select an option in a hierarchy. Out of the box, this module supports the taxonomy and content_taxonomy modules, but that…

Code

function _hierarchial_select_enforce_deepest_selection($lineage, $root_level, $module, $params) {
  $deepest_selection = end($lineage);

  // If no default is selected, thus the lineage is still empty, select the
  // first option of the root level by default.
  if (count($lineage) == 0) {
    $first_option = reset(array_keys($root_level));
    $lineage[] = $first_option;
    $deepest_selection = $first_option;
  }

  // Use the deepest selection 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 = $deepest_selection;
  $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;
}