You are here

function taxonomy_get_tree in Drupal 6

Same name and namespace in other branches
  1. 4 modules/taxonomy.module \taxonomy_get_tree()
  2. 5 modules/taxonomy/taxonomy.module \taxonomy_get_tree()
  3. 7 modules/taxonomy/taxonomy.module \taxonomy_get_tree()

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. Now deprecated and isn't used. It is left here only because of compatibility issues.

$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. Results are statically cached.

10 calls to taxonomy_get_tree()
forum_get_forums in modules/forum/forum.module
Returns a list of all forums for a given taxonomy id
forum_nodeapi in modules/forum/forum.module
Implementation of hook_nodeapi().
taxonomy_check_vocabulary_hierarchy in modules/taxonomy/taxonomy.module
Dynamically check and update the hierarachy flag of a vocabulary. Checks and updates 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.

... See full list

File

modules/taxonomy/taxonomy.module, line 848
Enables the organization of content into categories.

Code

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

  // 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();
    $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 = !isset($max_depth) ? count($children[$vid]) : $max_depth;
  $tree = array();

  // Keeps track of the parents we have to process, the last entry is used
  // for the next processing step.
  $process_parents = array();
  $process_parents[] = $parent;

  // Loops over the parent terms and adds its children to the tree array.
  // Uses a loop instead of a recursion, because it's more efficient.
  while (count($process_parents)) {
    $parent = array_pop($process_parents);

    // The number of parents determines the current depth.
    $depth = count($process_parents);
    if ($max_depth > $depth && !empty($children[$vid][$parent])) {
      $has_children = FALSE;
      $child = current($children[$vid][$parent]);
      do {
        if (empty($child)) {
          break;
        }
        $term = $terms[$vid][$child];
        if (count($parents[$vid][$term->tid]) > 1) {

          // We have a term with multi parents here. Clone the term,
          // so that the depth attribute remains correct.
          $term = clone $term;
        }
        $term->depth = $depth;
        unset($term->parent);
        $term->parents = $parents[$vid][$term->tid];
        $tree[] = $term;
        if (!empty($children[$vid][$term->tid])) {
          $has_children = TRUE;

          // We have to continue with this parent later.
          $process_parents[] = $parent;

          // Use the current term as parent for the next iteration.
          $process_parents[] = $term->tid;

          // Reset pointers for child lists because we step in there more often
          // with multi parents.
          reset($children[$vid][$term->tid]);

          // Move pointer so that we get the correct term the next time.
          next($children[$vid][$parent]);
          break;
        }
      } while ($child = next($children[$vid][$parent]));
      if (!$has_children) {

        // We processed all terms in this hierarchy-level, reset pointer
        // so that this function works the next time it gets called.
        reset($children[$vid][$parent]);
      }
    }
  }
  return $tree;
}