You are here

function _term_reference_tree_get_children in Taxonomy Term Reference Tree Widget 8

Same name and namespace in other branches
  1. 7.2 term_reference_tree.module \_term_reference_tree_get_children()

This function is like taxonomy_get_children, except it doesn't load the entire term.

Parameters

int $tid: The ID of the term whose children you want to get.

int $vid: The vocabulary ID.

Return value

array Taxonomy terms, each in the form ['tid' => $tid, 'name' => $name].

1 call to _term_reference_tree_get_children()
_term_reference_tree_get_term_hierarchy in ./term_reference_tree.module
This function returns a taxonomy term hierarchy in a nested array.

File

./term_reference_tree.module, line 219

Code

function _term_reference_tree_get_children($tid, $vid) {

  // DO NOT LOAD TAXONOMY TERMS HERE.
  // Taxonomy terms take a lot of time and memory to load, and this can be
  // very bad on large vocabularies.  Instead, we load the term as necessary
  // in cases where it's needed (such as using tokens or when the locale
  // module is enabled).
  $table = 'taxonomy_term_field_data';
  $alias = 't';
  $query = \Drupal::database()
    ->select($table, $alias);
  $query
    ->join('taxonomy_term__parent', 'p', 't.tid = p.entity_id');
  $query
    ->fields('t', [
    'tid',
    'name',
  ]);
  $query
    ->addField('t', 'vid', 'vocabulary_machine_name');
  $query
    ->condition('t.vid', $vid)
    ->condition('p.parent_target_id', $tid)
    ->addTag('term_access')
    ->addTag('translatable')
    ->addTag('term_reference_tree_get_children')
    ->orderBy('t.weight')
    ->orderBy('t.name');
  $result = $query
    ->execute();
  $terms = [];
  while ($term = $result
    ->fetchObject()) {
    $terms[$term->tid] = $term;
  }
  return $terms;
}