TermStorage::loadAllParents

public TermStorage::loadAllParents($tid)

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 143

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];
}
doc_Drupal
2016-10-29 09:47:22
Comments
Leave a Comment

Please login to continue.