You are here

function _hs_taxonomy_hierarchical_select_get_tree in Hierarchical Select 5.3

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

Drupal core's taxonomy_get_tree() doesn't allow us to reset the cached trees, which obviously causes problems when you create new items between two calls to it.

8 calls to _hs_taxonomy_hierarchical_select_get_tree()
hs_content_taxonomy_hierarchical_select_children in modules/hs_content_taxonomy.module
Implementation of hook_hierarchical_select_children().
hs_content_taxonomy_hierarchical_select_root_level in modules/hs_content_taxonomy.module
Implementation of hook_hierarchical_select_root_level().
hs_content_taxonomy_views_hierarchical_select_entity_count in modules/hs_content_taxonomy_views.module
Implementation of hook_hierarchical_select_entity_count().
hs_taxonomy_hierarchical_select_create_item in modules/hs_taxonomy.module
Implementation of hook_hierarchical_select_create_item().
hs_taxonomy_hierarchical_select_root_level in modules/hs_taxonomy.module
Implementation of hook_hierarchical_select_root_level().

... See full list

File

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

Code

function _hs_taxonomy_hierarchical_select_get_tree($vid, $parent = 0, $depth = -1, $max_depth = NULL, $reset = FALSE) {
  static $children, $parents, $terms;
  if ($reset) {
    $children = $parents = $terms = array();
  }
  $depth++;

  // We cache trees, so it's not CPU-intensive to call get_tree() on a term
  // and its children, too.
  if (!isset($children[$vid])) {
    $children[$vid] = array();
    $result = db_query(db_rewrite_sql('SELECT t.tid, t.*, parent FROM {term_data} t INNER JOIN  {term_hierarchy} h ON t.tid = h.tid WHERE t.vid = %d ORDER BY weight, name', 't', 'tid'), $vid);
    while ($term = db_fetch_object($result)) {
      $children[$vid][$term->parent][] = $term->tid;
      $parents[$vid][$term->tid][] = $term->parent;
      $terms[$vid][$term->tid] = $term;
    }
  }
  $max_depth = is_null($max_depth) ? count($children[$vid]) : $max_depth;
  if ($children[$vid][$parent]) {
    foreach ($children[$vid][$parent] as $child) {
      if ($max_depth > $depth) {
        $term = drupal_clone($terms[$vid][$child]);
        $term->depth = $depth;

        // The "parent" attribute is not useful, as it would show one parent only.
        unset($term->parent);
        $term->parents = $parents[$vid][$child];
        $tree[] = $term;
        if ($children[$vid][$child]) {
          $tree = array_merge($tree, _hs_taxonomy_hierarchical_select_get_tree($vid, $child, $depth, $max_depth));
        }
      }
    }
  }
  return $tree ? $tree : array();
}