function taxonomy_get_tree

cis7 taxonomy.module taxonomy_get_tree($vid, $parent = 0, $max_depth = NULL, $load_entities = FALSE)
cle7 taxonomy.module taxonomy_get_tree($vid, $parent = 0, $max_depth = NULL, $load_entities = FALSE)
elmsmedia7 taxonomy.module taxonomy_get_tree($vid, $parent = 0, $max_depth = NULL, $load_entities = FALSE)
icor7 taxonomy.module taxonomy_get_tree($vid, $parent = 0, $max_depth = NULL, $load_entities = FALSE)
meedjum_blog7 taxonomy.module taxonomy_get_tree($vid, $parent = 0, $max_depth = NULL, $load_entities = FALSE)
mooc7 taxonomy.module 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.

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

$load_entities: If TRUE, a full entity load will occur on the term objects. Otherwise they are partial objects queried directly from the {taxonomy_term_data} table to save execution time and memory consumption when listing large numbers of terms. Defaults to FALSE.

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. Term objects will be partial or complete depending on the $load_entities parameter.

22 calls to taxonomy_get_tree()
analytics_dashboard_charts in sites/all/modules/local_contrib/google_chart_tools/analytics_dashboard/analytics_dashboard.charts.inc
context_condition_node_taxonomy::condition_form in sites/all/modules/ulmus/context/plugins/context_condition_node_taxonomy.inc
Condition form.
context_condition_node_taxonomy::condition_values in sites/all/modules/ulmus/context/plugins/context_condition_node_taxonomy.inc
Condition values.
ctools_term_ctools_access_settings in sites/all/modules/ulmus/ctools/plugins/access/term.inc
Settings form for the 'by term' access plugin
ctools_term_has_parent_ctools_access_settings in sites/all/modules/ulmus/ctools/plugins/access/term_has_parent.inc
Settings form for the 'by parent term' access plugin

... See full list

1 string reference to 'taxonomy_get_tree'
taxonomy_terms_static_reset in modules/taxonomy/taxonomy.module
Clear all static cache variables for terms.

File

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

Code

function taxonomy_get_tree($vid, $parent = 0, $max_depth = NULL, $load_entities = FALSE) {
  $children = &drupal_static(__FUNCTION__, array());
  $parents = &drupal_static(__FUNCTION__ . ':parents', array());
  $terms = &drupal_static(__FUNCTION__ . ':terms', array());

  // We cache trees, so it's not CPU-intensive to call taxonomy_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('t.weight')->orderBy('t.name')->execute();

    foreach ($result as $term) {
      $children[$vid][$term->parent][] = $term->tid;
      $parents[$vid][$term->tid][] = $term->parent;
      $terms[$vid][$term->tid] = $term;
    }
  }

  // Load full entities, if necessary. The entity controller statically
  // caches the results.
  if ($load_entities) {
    $term_entities = taxonomy_term_load_multiple(array_keys($terms[$vid]));
  }

  $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 = $load_entities ? $term_entities[$child] : $terms[$vid][$child];
        if (isset($parents[$vid][$term->tid])) {
          // Clone the term so that the depth attribute remains correct
          // in the event of multiple parents.
          $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;
}
Error | ELMSLN API

Error

×

Error message

  • Warning: Cannot modify header information - headers already sent by (output started at /var/www/html/elmsln_community/api.elmsln.org/includes/common.inc:2791) in drupal_send_headers() (line 1499 of /var/www/html/elmsln_community/api.elmsln.org/includes/bootstrap.inc).
  • Error: Call to undefined function apc_delete() in DrupalAPCCache->clear() (line 289 of /var/www/html/elmsln_community/api.elmsln.org/sites/all/modules/apc/drupal_apc_cache.inc).
The website encountered an unexpected error. Please try again later.