You are here

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

Function that generates the nested list for the JSON array structure.

3 calls to TermReferenceFancytree::getNestedListJsonArray()
SubTreeController::json in src/Controller/SubTreeController.php
JSON callback for subtree.
TermReferenceFancytree::getTopLevelNodes in src/Element/TermReferenceFancytree.php
Function that returns the top level nodes for the tree.
TermReferenceFancytree::getVocabularyNamesJsonArray in src/Element/TermReferenceFancytree.php
Function that generates a list of vocabulary names in JSON.

File

src/Element/TermReferenceFancytree.php, line 242

Class

TermReferenceFancytree
Term Reference Tree Form Element.

Namespace

Drupal\term_reference_fancytree\Element

Code

public static function getNestedListJsonArray($terms, $element, $ancestors = NULL, $form_state = NULL) {
  $items = [];
  if (!empty($terms)) {
    foreach ($terms as $term) {
      $item = [
        'title' => Html::escape($term
          ->getName()),
        'key' => $term
          ->id(),
      ];

      // Checking the term against the form state and default values and if present, mark as
      // selected.
      if ($form_state && $form_state
        ->getUserInput()) {
        if (in_array($term
          ->id(), $form_state
          ->getValues()[$element['#field_name']])) {
          $item['selected'] = TRUE;
        }
      }
      elseif (isset($element['#default_value']) && is_numeric(array_search($term
        ->id(), array_column($element['#default_value'], 'target_id')))) {
        $item['selected'] = TRUE;
      }

      // If the term is an ancestor we will want to add it to the tree instead
      // of marking it as lazy load.
      if (isset($ancestors[$term
        ->id()])) {

        // We add an active trail class to the item.
        $item['extraClasses'] = "activeTrail";

        // We load all the children and pass it to this function recursively.
        $children = \Drupal::entityTypeManager()
          ->getStorage('taxonomy_term')
          ->loadChildren($term
          ->id());
        $child_items = self::getNestedListJsonArray($children, $element, $ancestors, $form_state);

        // If we get some children, we add those under the item.
        if ($child_items) {
          $item['children'] = $child_items;
        }
      }
      elseif (isset($term->children) || TermReferenceFancytree::getChildCount($term
        ->id()) >= 1) {

        // If the given terms array is nested, directly process the terms.
        if (isset($term->children)) {
          $item['children'] = TermReferenceFancytree::getNestedListJsonArray($term->children, $element, $form_state);
        }
        else {
          $item['lazy'] = TRUE;
        }
      }
      $items[] = $item;
    }
  }
  return $items;
}