You are here

function _patterns_taxonomy_get_tree in Patterns 6.2

Same name and namespace in other branches
  1. 6 patterns.module \_patterns_taxonomy_get_tree()

Custom implementation of Drupal's taxonomy_get_tree()

Removed static caching. New terms may be created during patterns execution and static caching prevents them from being returned in all subsequent calls to taxonomy_get_tree() during the current pattern execution (within current page request)

2 calls to _patterns_taxonomy_get_tree()
patterns_form_alter in ./patterns.module
_patterns_taxonomy_term_select in ./patterns.module
Custom implementation of Drupal's _taxonomy_term_select()

File

./patterns.module, line 3100
Enables extremely simple adding/removing features to your site with minimal to no configuration

Code

function _patterns_taxonomy_get_tree($vid, $parent = 0, $depth = -1, $max_depth = NULL) {
  $depth++;
  $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;
  $tree = array();
  if (!empty($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 (!empty($children[$vid][$child])) {
          $tree = array_merge($tree, _patterns_taxonomy_get_tree($vid, $child, $depth, $max_depth));
        }
      }
    }
  }
  return $tree;
}