diff --git a/core/modules/taxonomy/src/TermForm.php b/core/modules/taxonomy/src/TermForm.php index af287ba8813182c025b9e275bdce5e09fe322c89..d5966ccf017918673b7dc67ab2c37e25d40137bb 100644 --- a/core/modules/taxonomy/src/TermForm.php +++ b/core/modules/taxonomy/src/TermForm.php @@ -23,12 +23,7 @@ public function form(array $form, FormStateInterface $form_state) { $taxonomy_storage = $this->entityTypeManager->getStorage('taxonomy_term'); $vocabulary = $vocab_storage->load($term->bundle()); - $parent = []; - // Get the parent directly from the term as - // \Drupal\taxonomy\TermStorageInterface::loadParents() excludes the root. - foreach ($term->get('parent') as $item) { - $parent[] = (int) $item->target_id; - } + $parent = $this->getParentIds($term); $form_state->set(['taxonomy', 'parent'], $parent); $form_state->set(['taxonomy', 'vocabulary'], $vocabulary); @@ -219,4 +214,23 @@ public function save(array $form, FormStateInterface $form_state) { $form_state->set('tid', $term->id()); } + /** + * Returns term parent IDs, including the root. + * + * @param \Drupal\taxonomy\TermInterface $term + * The taxonomy term entity. + * + * @return array + * A list if parent term IDs. + */ + protected function getParentIds(TermInterface $term): array { + $parent = []; + // Get the parent directly from the term as + // \Drupal\taxonomy\TermStorageInterface::loadParents() excludes the root. + foreach ($term->get('parent') as $item) { + $parent[] = (int) $item->target_id; + } + return $parent; + } + }