diff --git a/core/modules/taxonomy/src/Entity/Term.php b/core/modules/taxonomy/src/Entity/Term.php index 8cbfdda..a4435dd 100644 --- a/core/modules/taxonomy/src/Entity/Term.php +++ b/core/modules/taxonomy/src/Entity/Term.php @@ -73,12 +73,14 @@ public static function postDelete(EntityStorageInterface $storage, array $entiti /** @var \Drupal\taxonomy\TermInterface $term */ foreach ($entities as $tid => $term) { if ($children = $term->getChildren()) { + /** @var \Drupal\taxonomy\TermInterface $child */ foreach ($children as $child) { + $parent = $child->get('parent'); // Update child parents item list. - $child->getParents()->removeItemsByTargetId($tid); + $parent->removeItemsByTargetId($tid); // If the term has multiple parents, we don't delete it. - if ($child->getParents()->count()) { + if ($parent->count()) { $child->save(); } else { @@ -99,7 +101,7 @@ public static function postDelete(EntityStorageInterface $storage, array $entiti public function preSave(EntityStorageInterface $storage) { parent::preSave($storage); // Terms with no parents are mandatory children of . - if (!$this->getParents()->count()) { + if (!$this->get('parent')->count()) { $this->parent->target_id = 0; } } @@ -244,7 +246,18 @@ public function getVocabularyId() { * {@inheritdoc} */ public function getParents() { - return $this->get('parent'); + $parents = $ids = []; + // Cannot use $this->get('parent')->referencedEntities() here because that + // strips out the '0' reference. + foreach ($this->get('parent') as $item) { + if ($item->target_id == 0) { + // The parent. + $parents[0] = NULL; + continue; + } + $ids[] = $item->target_id; + } + return $parents + static::loadMultiple($ids); } /** @@ -256,10 +269,10 @@ public function getAncestors() { $search[] = $this->id(); while ($tid = array_shift($search)) { - foreach (static::load($tid)->getParents() as $parent) { - if (!empty($parent->entity) && !isset($this->ancestors[$parent->target_id])) { - $this->ancestors[$parent->target_id] = $parent->entity; - $search[] = $parent->target_id; + foreach (static::load($tid)->getParents() as $id => $parent) { + if (!empty($parent) && !isset($this->ancestors[$id])) { + $this->ancestors[$id] = $parent; + $search[] = $id; } } } @@ -270,12 +283,9 @@ public function getAncestors() { /** * {@inheritdoc} */ - public function getChildren($vid = NULL) { + public function getChildren() { $query = \Drupal::entityQuery('taxonomy_term') ->condition('parent', $this->id()); - if ($vid) { - $query->condition('vid', $vid); - } return static::loadMultiple($query->execute()); } diff --git a/core/modules/taxonomy/src/TermHierarchyInterface.php b/core/modules/taxonomy/src/TermHierarchyInterface.php index 98f3054..b4de45a 100644 --- a/core/modules/taxonomy/src/TermHierarchyInterface.php +++ b/core/modules/taxonomy/src/TermHierarchyInterface.php @@ -12,9 +12,9 @@ /** * Returns a list of parents of this term. * - * @return \Drupal\Core\Field\EntityReferenceFieldItemListInterface - * A list of references to parents taxonomy terms. If this term has a - * parent that item is keyed with 0 and have NULL as value. + * @return \Drupal\taxonomy\TermInterface[] + * The parent taxonomy term entities keyed by term id. If this term has a + * parent, that item is keyed with 0 and will have NULL as value. */ public function getParents(); @@ -29,12 +29,9 @@ public function getAncestors(); /** * Returns all children terms of this term. * - * @param string $vid - * An optional vocabulary ID to restrict the child search. - * * @return \Drupal\taxonomy\TermInterface[] * A list of children taxonomy term entities keyed by term id. */ - public function getChildren($vid = NULL); + public function getChildren(); } diff --git a/core/modules/taxonomy/src/TermStorage.php b/core/modules/taxonomy/src/TermStorage.php index 34bf462..326b76a 100644 --- a/core/modules/taxonomy/src/TermStorage.php +++ b/core/modules/taxonomy/src/TermStorage.php @@ -83,11 +83,11 @@ public function loadParents($tid) { $terms = []; /** @var \Drupal\taxonomy\TermInterface $term */ if ($tid && $term = $this->load($tid)) { - foreach ($term->getParents() as $delta => $item) { + foreach ($term->getParents() as $id => $parent) { // This method currently doesn't return the parent. // @see https://www.drupal.org/node/2019905 - if (!empty($item->entity)) { - $terms[$item->target_id] = $item->entity; + if (!empty($id)) { + $terms[$id] = $parent; } } } @@ -108,7 +108,7 @@ public function loadAllParents($tid) { */ public function loadChildren($tid, $vid = NULL) { /** @var \Drupal\taxonomy\TermInterface $term */ - return (!empty($tid) && $term = $this->load($tid)) ? $term->getChildren($vid) : []; + return (!empty($tid) && $term = $this->load($tid)) ? $term->getChildren() : []; } /**