You are here

public function TermStorage::loadAllParents in Zircon Profile 8

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

Finds all ancestors of a given term ID.

Parameters

int $tid: Term ID to retrieve ancestors for.

Return value

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

Overrides TermStorageInterface::loadAllParents

File

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

Class

TermStorage
Defines a Controller class for taxonomy terms.

Namespace

Drupal\taxonomy

Code

public function loadAllParents($tid) {
  if (!isset($this->parentsAll[$tid])) {
    $parents = array();
    if ($term = $this
      ->load($tid)) {
      $parents[$term
        ->id()] = $term;
      $terms_to_search[] = $term
        ->id();
      while ($tid = array_shift($terms_to_search)) {
        if ($new_parents = $this
          ->loadParents($tid)) {
          foreach ($new_parents as $new_parent) {
            if (!isset($parents[$new_parent
              ->id()])) {
              $parents[$new_parent
                ->id()] = $new_parent;
              $terms_to_search[] = $new_parent
                ->id();
            }
          }
        }
      }
    }
    $this->parentsAll[$tid] = $parents;
  }
  return $this->parentsAll[$tid];
}