You are here

public function TermStorage::loadChildren in Zircon Profile 8.0

Same name and namespace in other branches
  1. 8 core/modules/taxonomy/src/TermStorage.php \Drupal\taxonomy\TermStorage::loadChildren()

Finds all children of a term ID.

Parameters

int $tid: Term ID to retrieve parents for.

string $vid: An optional vocabulary ID to restrict the child search.

Return value

\Drupal\taxonomy\TermInterface[] An array of term objects that are the children of the term $tid.

Overrides TermStorageInterface::loadChildren

File

core/modules/taxonomy/src/TermStorage.php, line 175
Contains \Drupal\taxonomy\TermStorage.

Class

TermStorage
Defines a Controller class for taxonomy terms.

Namespace

Drupal\taxonomy

Code

public function loadChildren($tid, $vid = NULL) {
  if (!isset($this->children[$tid])) {
    $children = array();
    $query = $this->database
      ->select('taxonomy_term_field_data', 't');
    $query
      ->join('taxonomy_term_hierarchy', 'h', 'h.tid = t.tid');
    $query
      ->addField('t', 'tid');
    $query
      ->condition('h.parent', $tid);
    if ($vid) {
      $query
        ->condition('t.vid', $vid);
    }
    $query
      ->condition('t.default_langcode', 1);
    $query
      ->addTag('term_access');
    $query
      ->orderBy('t.weight');
    $query
      ->orderBy('t.name');
    if ($ids = $query
      ->execute()
      ->fetchCol()) {
      $children = $this
        ->loadMultiple($ids);
    }
    $this->children[$tid] = $children;
  }
  return $this->children[$tid];
}