You are here

public static function TermReferenceFancytree::getVocabularyNamesJsonArray 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::getVocabularyNamesJsonArray()

Function that generates a list of vocabulary names in JSON.

1 call to TermReferenceFancytree::getVocabularyNamesJsonArray()
TermReferenceFancytree::getTopLevelNodes in src/Element/TermReferenceFancytree.php
Function that returns the top level nodes for the tree.

File

src/Element/TermReferenceFancytree.php, line 299

Class

TermReferenceFancytree
Term Reference Tree Form Element.

Namespace

Drupal\term_reference_fancytree\Element

Code

public static function getVocabularyNamesJsonArray($element, $ancestors = NULL, $form_state) {
  $items = [];
  $vocabularies = $element['#vocabulary'];
  if (!empty($vocabularies)) {
    foreach ($vocabularies as $vocabulary) {
      $item = [
        'title' => Html::escape($vocabulary
          ->get('name')),
        'key' => $vocabulary
          ->id(),
        'vocab' => TRUE,
        'unselectable' => TRUE,
        'folder' => TRUE,
      ];

      // Load child terms for the vocabulary.
      $terms = TermReferenceFancytree::loadTerms($vocabulary, 0);

      // For each term, check if it's an ancestor of a selected item.
      // If it is, then we need to load the vocabulary folder.
      foreach ($terms as $term) {
        if (isset($ancestors[$term
          ->id()])) {
          $item['lazy'] = FALSE;
          break;
        }
        else {
          $item['lazy'] = TRUE;
        }
      }

      // If the vocabulary folder is loaded, we add the active trail and
      // load its children.
      if (!$item['lazy']) {
        $item['extraClasses'] = 'activeTrail';
        $item['children'] = TermReferenceFancytree::getNestedListJsonArray($terms, $element, $ancestors, $form_state);
      }
      $items[] = $item;
    }
  }
  return $items;
}