/**
  * 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);
 }
 /**
  * Returns partial tree for a given path
  */
 function getPartialTree($path, $depth = 0)
 {
     $tree = array();
     $parent = $path[$depth];
     $children = \Drupal::entityTypeManager()->getStorage('taxonomy_term')->loadChildren($parent->id());
     if (isset($path[++$depth])) {
         $next_term = $path[$depth];
     }
     $index = 0;
     foreach ($children as $child) {
         $child->depth = $depth;
         $child->parents = array(0 => $parent->tid);
         $tree[] = array('title' => $child->getName(), 'key' => $child->id(), 'expanded' => TRUE, 'selected' => TRUE);
         if (isset($next_term) && $child->id() == $next_term->id()) {
             $tree[$index]['children'] = TaxonomyManagerTree::getPartialTree($path, $depth);
         }
         $index++;
     }
     return $tree;
 }