You are here

function hs_taxonomy_hierarchical_select_children in Hierarchical Select 7.3

Same name and namespace in other branches
  1. 5.3 modules/hs_taxonomy.module \hs_taxonomy_hierarchical_select_children()
  2. 6.3 modules/hs_taxonomy.module \hs_taxonomy_hierarchical_select_children()

Implementation of hook_hierarchical_select_children().

File

modules/hs_taxonomy.module, line 661
Implementation of the Hierarchical Select API for the Taxonomy module.

Code

function hs_taxonomy_hierarchical_select_children($parent, $params) {
  static $tree;
  $depth = $params['allowed_max_depth'];
  $vid = $params['vid'];
  if (isset($params['root_term']) && $params['root_term'] && $parent == 0 || !is_numeric($parent)) {
    return array();
  }
  $terms = taxonomy_get_tree($params['vid'], $parent, 1);

  // Unset the term that's being excluded, if it is among the children.
  if (isset($params['exclude_tid'])) {
    unset($terms[$params['exclude_tid']]);
  }

  // If the Term Permissions module is installed, honor its settings.
  if (module_exists('term_permissions')) {
    global $user;
    foreach ($terms as $key => $term) {
      if (!term_permissions_allowed($term->tid, $user)) {
        unset($terms[$key]);
      }
    }
  }

  // Keep a static cache of the entire tree, this allows us to quickly look up
  // if a term is not too deep – because if it's too deep, we don't want to
  // return any children.
  if (!isset($tree[$vid])) {
    $raw_tree = _hs_taxonomy_hierarchical_select_get_tree($vid);
    foreach ($raw_tree as $term) {
      $tree[$vid][$term->tid] = $term->depth;
    }
  }
  $terms = $depth > 0 && $tree[$vid][$parent] + 1 >= $depth ? array() : _hs_taxonomy_hierarchical_select_get_tree($vid, $parent, -1, 1);
  return _hs_taxonomy_hierarchical_select_terms_to_options($terms);
}