Commit ea0c86bc authored by catch's avatar catch
Browse files

Issue #3332877 by claudiu.cristea, xjm, penyaskito, alexpott: Added...

Issue #3332877 by claudiu.cristea, xjm, penyaskito, alexpott: Added TermForm::getParentIds for allowing to override in contrib

(cherry picked from commit 20e21dc4)
parent b1c9aab1
Loading
Loading
Loading
Loading
+20 −6
Original line number Diff line number Diff line
@@ -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;
  }

}