/** * 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); }
/** * Helper function that generates the nested list for the JSON array structure. */ public static function getNestedListJSONArray($terms) { $items = array(); if (!empty($terms)) { foreach ($terms as $term) { $item = array('title' => HTML::escape($term->getName()), 'key' => $term->id()); if (isset($term->children) || TaxonomyManagerTree::getChildCount($term->id()) >= 1) { // If the given terms array is nested, directly process the terms. if (isset($term->children)) { $item['children'] = TaxonomyManagerTree::getNestedListJSONArray($term->children); } else { $item['lazy'] = TRUE; } } $items[] = $item; } } return $items; }