taxonomy_get_tree

Versions
4.6 – 6
taxonomy_get_tree($vid, $parent = 0, $depth = -1, $max_depth = NULL)
7
taxonomy_get_tree($vid, $parent = 0, $max_depth = NULL, $depth = -1)

Create a hierarchical representation of a vocabulary.

Parameters

$vid Which vocabulary to generate the tree for.

$parent The term ID under which to generate the tree. If 0, generate the tree for the entire vocabulary.

$max_depth The number of levels of the tree to return. Leave NULL to return all levels.

$depth Internal use only.

Return value

An array of all term objects in the tree. Each term object is extended to have "depth" and "parents" attributes in addition to its normal ones. Results are statically cached.

▾ 9 functions call taxonomy_get_tree()

forum_get_forums in modules/forum/forum.module
Returns a list of all forums for a given taxonomy id
taxonomy_allowed_values in modules/taxonomy/taxonomy.module
Create an array of the allowed values for this field.
taxonomy_check_vocabulary_hierarchy in modules/taxonomy/taxonomy.module
Dynamically check and update the hierarchy flag of a vocabulary.
taxonomy_form_all in modules/taxonomy/taxonomy.module
Generate a set of options for selecting a term from all vocabularies.
taxonomy_form_term in modules/taxonomy/taxonomy.admin.inc
Form function for the term edit form.
taxonomy_get_tree in modules/taxonomy/taxonomy.module
Create a hierarchical representation of a vocabulary.
taxonomy_overview_terms in modules/taxonomy/taxonomy.admin.inc
Form builder for the taxonomy terms overview.
taxonomy_overview_terms_submit in modules/taxonomy/taxonomy.admin.inc
Submit handler for terms overview form.
_forum_parent_select in modules/forum/forum.admin.inc
Returns a select box for available parent terms

Code

modules/taxonomy/taxonomy.module, line 660

<?php
function taxonomy_get_tree($vid, $parent = 0, $max_depth = NULL, $depth = -1) {
  $children = &drupal_static(__FUNCTION__, array());
  $parents = &drupal_static(__FUNCTION__ . 'parents', array());
  $terms = &drupal_static(__FUNCTION__ . '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();
    $parents[$vid] = array();
    $terms[$vid] = array();

    $query = db_select('taxonomy_term_data', 't');
    $query->join('taxonomy_term_hierarchy', 'h', 'h.tid = t.tid');
    $result = $query
      ->addTag('translatable')
      ->addTag('term_access')
      ->fields('t')
      ->fields('h', array('parent'))
      ->condition('t.vid', $vid)
      ->orderBy('weight')
      ->orderBy('name')
      ->execute();
    foreach ($result as $term) {
      $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;
  $tree = array();
  if ($max_depth > $depth && !empty($children[$vid][$parent])) {
    foreach ($children[$vid][$parent] as $child) {
      $term = 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 (!empty($children[$vid][$child])) {
        $tree = array_merge($tree, taxonomy_get_tree($vid, $child, $max_depth, $depth));
      }
    }
  }

  return $tree;
}
?>
Login or register to post comments
 
 

All source code and documentation on this site is released under the terms of the GNU General Public License, version 2 and later. Drupal is a registered trademark of Dries Buytaert.