You are here

function _hs_smallhierarchy_transform in Hierarchical Select 5.3

Same name and namespace in other branches
  1. 6.3 modules/hs_smallhierarchy.module \_hs_smallhierarchy_transform()
  2. 7.3 modules/hs_smallhierarchy.module \_hs_smallhierarchy_transform()

Automatically transform a given hierarchy with this format: array( 'win' => array( 'label' => 'Windows', 'children' => array( 'xp' => array('label' => 'XP'), 'vista' => array( 'label' => 'Vista', 'children' => array( 'x86' => array('label' => '32-bits'), 'x64' => array('label' => '64-bits'), ), ), ), ), )

to one with this format: array( 'root' => array( 'children' => array( 'xp', 'vista', ), ), 'win' => array( 'label' => 'Windows', 'children' => array( 'win|xp', 'win|vista', ), ), 'win|xp' => array( 'label' => 'XP', ), 'win|vista' => array( 'label' => 'Vista', 'children' => array( 'win|vista|x86', 'win|vista|x64', ), ), 'win|vista|x86' => array( 'label' => '32-bits', ), 'win|vista|x64' => array( 'label' => '64-bits', ), )

This new format:

  • ensures unique identifiers for each item
  • makes it very easy to find the parent of a given item.
  • makes it very easy to find the label and children of a given item.

@params $hierarchy The hierarchy. @params $id A unique identifier for the hierarchy, for caching purposes. @params $separator The separator to use.

4 calls to _hs_smallhierarchy_transform()
hs_smallhierarchy_hierarchical_select_children in modules/hs_smallhierarchy.module
Implementation of hook_hierarchical_select_children().
hs_smallhierarchy_hierarchical_select_item_get_label in modules/hs_smallhierarchy.module
Implementation of hook_hierarchical_select_item_get_label().
hs_smallhierarchy_hierarchical_select_root_level in modules/hs_smallhierarchy.module
Implementation of hook_hierarchical_select_root_level().
hs_smallhierarchy_hierarchical_select_valid_item in modules/hs_smallhierarchy.module
Implementation of hook_hierarchical_select_valid_item().

File

modules/hs_smallhierarchy.module, line 171
Implementation of the Hierarchical Select API that allows one to use a hardcoded hierarchy. When it becomes to slow, you should move the hierarchy into the database and write a proper implementation.

Code

function _hs_smallhierarchy_transform($hierarchy, $id, $separator = '|') {

  // Make sure each hierarchy is only transformed once.
  if (!isset($hs_hierarchy[$id])) {
    $hs_hierarchy[$id] = array();

    // Build the root level.
    foreach ($hierarchy as $item => $children) {
      $hs_hierarchy[$id]['root']['children'][] = $item;
      $hs_hierarchy[$id][$item]['label'] = $children['label'];

      // Build the subsequent levels.
      if (isset($children['children'])) {
        _hs_smallhierarchy_transform_recurse($item, $hs_hierarchy[$id], $children['children'], $separator);
      }
    }
  }
  return $hs_hierarchy[$id];
}