taxonomy_get_tree

  1. drupal
    1. 4.7
    2. 5 taxonomy.module
    3. 6 taxonomy.module
    4. 7 taxonomy.module
Versions
4.7 – 6 taxonomy_get_tree($vid, $parent = 0, $depth = -1, $max_depth = NULL)
7 taxonomy_get_tree($vid, $parent = 0, $max_depth = NULL, $load_entities = FALSE)

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.

$depth Internal use only.

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

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.

▾ 10 functions call taxonomy_get_tree()

forum_get_forums in modules/forum.module
Returns a list of all forums for a given taxonomy id
forum_overview in modules/forum.module
Returns an overview list of existing forums and containers
forum_submit in modules/forum.module
Implementation of hook_submit().
taxonomy_form_all in modules/taxonomy.module
Generate a set of options for selecting a term from all vocabularies. Can be passed to form_select.
taxonomy_form_term in modules/taxonomy.module
taxonomy_get_tree in modules/taxonomy.module
Create a hierarchical representation of a vocabulary.
taxonomy_overview_terms in modules/taxonomy.module
Display a tree of all the terms in a vocabulary, with options to edit each one.
taxonomy_select_nodes in modules/taxonomy.module
Finds all nodes that match selected taxonomy conditions.
_forum_parent_select in modules/forum.module
Returns a select box for available parent terms
_taxonomy_term_select in modules/taxonomy.module

Code

modules/taxonomy.module, line 882

<?php
function taxonomy_get_tree($vid, $parent = 0, $depth = -1, $max_depth = NULL) {
  static $children, $parents, $terms;

  $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) {
        $terms[$vid][$child]->depth = $depth;
        // The "parent" attribute is not useful, as it would show one parent only.
        unset($terms[$vid][$child]->parent);
        $terms[$vid][$child]->parents = $parents[$vid][$child];
        $tree[] = $terms[$vid][$child];

        if ($children[$vid][$child]) {
          $tree = array_merge($tree, taxonomy_get_tree($vid, $child, $depth, $max_depth));
        }
      }
    }
  }

  return $tree ? $tree : array();
}
?>