You are here

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

Same name and namespace in other branches
  1. 8.2 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.

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 117

Class

TermReferenceFancytree
Term Reference Tree Form Element.

Namespace

Drupal\term_reference_fancytree\Element

Code

public static function getTopLevelNodes(array $element, array $ancestors, $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, $ancestors, $form_state);
  }
  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);
  }
}