You are here

function i18n_taxonomy_get_tree in Internationalization 7

Get taxonomy tree for a given language

Parameters

$vid: Vocabulary id

$lang: Language code

$parent: Parent term id for the tree

1 call to i18n_taxonomy_get_tree()
i18n_taxonomy_allowed_values in i18n_taxonomy/i18n_taxonomy.module
Returns the set of valid terms for a taxonomy field.

File

i18n_taxonomy/i18n_taxonomy.module, line 1114
i18n taxonomy module

Code

function i18n_taxonomy_get_tree($vid, $langcode, $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 get_tree() on a term
  // and its children, too.
  if (!isset($children[$vid][$langcode])) {
    $children[$vid][$langcode] = array();
    $parents[$vid][$langcode] = array();
    $terms[$vid][$langcode] = array();
    $query = db_select('taxonomy_term_data', 't');
    $query
      ->join('taxonomy_term_hierarchy', 'h', 'h.tid = t.tid');
    $result = $query
      ->addTag('translatable')
      ->addTag('taxonomy_term_access')
      ->fields('t')
      ->fields('h', array(
      'parent',
    ))
      ->condition('t.vid', $vid)
      ->condition('t.language', $langcode)
      ->orderBy('t.weight')
      ->orderBy('t.name')
      ->execute();
    foreach ($result as $term) {
      $children[$vid][$langcode][$term->parent][] = $term->tid;
      $parents[$vid][$langcode][$term->tid][] = $term->parent;
      $terms[$vid][$langcode][$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][$langcode]));
  }
  $max_depth = !isset($max_depth) ? count($children[$vid][$langcode]) : $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][$langcode][$parent])) {
      $has_children = FALSE;
      $child = current($children[$vid][$langcode][$parent]);
      do {
        if (empty($child)) {
          break;
        }
        $term = $load_entities ? $term_entities[$child] : $terms[$vid][$langcode][$child];
        if (count($parents[$vid][$langcode][$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][$langcode][$term->tid];
        $tree[] = $term;
        if (!empty($children[$vid][$langcode][$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][$langcode][$term->tid]);

          // Move pointer so that we get the correct term the next time.
          next($children[$vid][$langcode][$parent]);
          break;
        }
      } while ($child = next($children[$vid][$langcode][$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][$langcode][$parent]);
      }
    }
  }
  return $tree;
}