You are here

function _term_reference_tree_taxonomy_get_tree in Taxonomy Term Reference Tree Widget 7

Gets all terms and their parent and children hierarchy for a given vid.

Parameters

int $vid: The taxonomy vocabulary ID.

Return value

array An array containing all terms, children and parent hierarchy.

2 calls to _term_reference_tree_taxonomy_get_tree()
_term_reference_tree_get_term_hierarchy in ./term_reference_tree.module
This function returns a taxonomy term hierarchy in a nested array.
_term_reference_tree_taxonomy_get_parents_all in ./term_reference_tree.module
Gets all parent tids for a given vid and children tids.

File

./term_reference_tree.module, line 133

Code

function _term_reference_tree_taxonomy_get_tree($vid) {
  $tree =& drupal_static(__FUNCTION__, array());
  if (!isset($tree[$vid])) {
    $query = db_select('taxonomy_term_data', 't');
    $query
      ->join('taxonomy_term_hierarchy', 'h', 'h.tid = t.tid');
    $query
      ->join('taxonomy_vocabulary', 'v', 't.vid = v.vid');
    $query
      ->addField('v', 'machine_name', 'vocabulary_machine_name');
    $result = $query
      ->addTag('translatable')
      ->addTag('taxonomy_term_access')
      ->fields('t')
      ->fields('h', array(
      'parent',
    ))
      ->condition('t.vid', $vid)
      ->orderBy('t.weight')
      ->orderBy('t.name')
      ->execute();
    foreach ($result as $term) {
      $tree[$vid]['children'][$term->parent][] = $term->tid;
      $tree[$vid]['parents'][$term->tid][] = $term->parent;
      $tree[$vid]['terms'][$term->tid] = $term;
    }
  }
  drupal_alter('term_reference_get_children', $tree[$vid]);
  return $tree[$vid];
}