You are here

public static function TermReferenceFancytree::getTopLevelNodes in Term Reference Fancytree 8.2

Same name and namespace in other branches
  1. 8 src/Element/TermReferenceFancytree.php \Drupal\term_reference_fancytree\Element\TermReferenceFancytree::getTopLevelNodes()

Function that returns the top level nodes for the tree.

If multiple vocabularies, it will return the vocabulary names, otherwise it will return the top level terms.

Parameters

array $element: The form element.

array $ancestors: The list of term ancestors for default values.

\Drupal\Core\Form\FormStateInterface $form_state: The form state.

Return value

array The nested JSON array with the top level nodes.

1 call to TermReferenceFancytree::getTopLevelNodes()
TermReferenceFancytree::processTree in src/Element/TermReferenceFancytree.php

File

src/Element/TermReferenceFancytree.php, line 130

Class

TermReferenceFancytree
Term Reference Tree Form Element.

Namespace

Drupal\term_reference_fancytree\Element

Code

public static function getTopLevelNodes(array $element, array $ancestors, FormStateInterface $form_state) {

  // If we have more than one vocabulary, we load the vocabulary names as
  // the initial level.
  if (count($element['#vocabulary']) > 1) {
    return TermReferenceFancytree::getVocabularyNamesJsonArray($element, $form_state, $ancestors);
  }
  else {
    $taxonomy_vocabulary = \Drupal::entityTypeManager()
      ->getStorage('taxonomy_vocabulary')
      ->load(reset($element['#vocabulary'])
      ->id());

    // Load the terms in the first level.
    $terms = TermReferenceFancytree::loadTerms($taxonomy_vocabulary, 0);

    // Convert the terms list into the Fancytree JSON format.
    return TermReferenceFancytree::getNestedListJsonArray($terms, $element, $ancestors, $form_state);
  }
}