/**
  * JSON callback for subtree.
  *
  * @return \Symfony\Component\HttpFoundation\JsonResponse
  *   JSON object with list of terms.
  */
 public function json()
 {
     $list = array();
     $parent = $this->request->get('parent');
     $term = $this->entityTypeManager()->getStorage('taxonomy_term')->load($parent);
     if ($term) {
         $taxonomy_vocabulary = $this->entityTypeManager()->getStorage('taxonomy_vocabulary')->load($term->getVocabularyId());
         if ($taxonomy_vocabulary) {
             $terms = TaxonomyManagerTree::loadTerms($taxonomy_vocabulary, $parent);
             $list = TaxonomyManagerTree::getNestedListJSONArray($terms);
         }
     }
     return new JsonResponse($list);
 }
 public static function processTree(&$element, FormStateInterface $form_state, &$complete_form)
 {
     $element['#tree'] = TRUE;
     if (!empty($element['#vocabulary'])) {
         $taxonomy_vocabulary = \Drupal::entityTypeManager()->getStorage('taxonomy_vocabulary')->load($element['#vocabulary']);
         $pager_size = isset($element['#pager_size']) ? $element['#pager_size'] : -1;
         $terms = TaxonomyManagerTree::loadTerms($taxonomy_vocabulary, 0, $pager_size);
         $list = TaxonomyManagerTree::getNestedListJSONArray($terms);
         // Expand tree to given terms.
         if (isset($element['#terms_to_expand'])) {
             $terms_to_expand = is_array($element['#terms_to_expand']) ? $element['#terms_to_expand'] : array($element['#terms_to_expand']);
             foreach ($terms_to_expand as $term_to_expand) {
                 TaxonomyManagerTree:
                 self::getFirstPath($term_to_expand, $list);
             }
         }
         $element['#attached']['library'][] = 'taxonomy_manager/tree';
         $element['#attached']['drupalSettings']['taxonomy_manager']['tree'][] = array('id' => $element['#id'], 'name' => $element['#name'], 'source' => $list);
         $element['tree'] = array();
         $element['tree']['#prefix'] = '<div id="' . $element['#id'] . '">';
         $element['tree']['#suffix'] = '</div>';
     }
     return $element;
 }