function TermStorage::getAncestors

Same name and namespace in other branches
  1. 9 core/modules/taxonomy/src/TermStorage.php \Drupal\taxonomy\TermStorage::getAncestors()
  2. 8.9.x core/modules/taxonomy/src/TermStorage.php \Drupal\taxonomy\TermStorage::getAncestors()
  3. 11.x core/modules/taxonomy/src/TermStorage.php \Drupal\taxonomy\TermStorage::getAncestors()

Returns all ancestors of this term.

@internal @todo Refactor away when TreeInterface is introduced.

Return value

\Drupal\taxonomy\TermInterface[] A list of ancestor taxonomy term entities keyed by term ID.

1 call to TermStorage::getAncestors()
TermStorage::loadAllParents in core/modules/taxonomy/src/TermStorage.php
Finds all ancestors of a given term ID.

File

core/modules/taxonomy/src/TermStorage.php, line 180

Class

TermStorage
Defines a Controller class for taxonomy terms.

Namespace

Drupal\taxonomy

Code

protected function getAncestors(TermInterface $term) {
  if (!isset($this->ancestors[$term->id()])) {
    $this->ancestors[$term->id()] = [
      $term->id() => $term,
    ];
    $search[] = $term->id();
    while ($tid = array_shift($search)) {
      foreach ($this->getParents(static::load($tid)) as $id => $parent) {
        if ($parent && !isset($this->ancestors[$term->id()][$id])) {
          $this->ancestors[$term->id()][$id] = $parent;
          $search[] = $id;
        }
      }
    }
  }
  return $this->ancestors[$term->id()];
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.