You are here

public function SubTreeController::json in Term Reference Fancytree 8

Same name and namespace in other branches
  1. 8.2 src/Controller/SubTreeController.php \Drupal\term_reference_fancytree\Controller\SubTreeController::json()

JSON callback for subtree.

Return value

\Symfony\Component\HttpFoundation\JsonResponse JSON object with list of terms.

1 string reference to 'SubTreeController::json'
term_reference_fancytree.routing.yml in ./term_reference_fancytree.routing.yml
term_reference_fancytree.routing.yml

File

src/Controller/SubTreeController.php, line 50

Class

SubTreeController
Exposes a list of terms to any JS library via JSON.

Namespace

Drupal\term_reference_fancytree\Controller

Code

public function json() {
  $list = [];

  // The parent being expanded.
  $parent = $this->request
    ->get('parent');

  // Flag to indicate if the parent is a vocabulary instead of a term.
  $vocab = $this->request
    ->get('vocab');

  // If the parent is a vocabulary, we want to load the first level of terms
  // of that vocabulary.
  if ($vocab) {
    $taxonomy_vocabulary = \Drupal::entityTypeManager()
      ->getStorage('taxonomy_vocabulary')
      ->load($parent);
    $terms = TermReferenceFancytree::loadTerms($taxonomy_vocabulary, 0, -1);
    $list = TermReferenceFancytree::getNestedListJsonArray($terms, []);
  }
  else {
    $term = $this
      ->entityTypeManager()
      ->getStorage('taxonomy_term')
      ->load($parent);
    if ($term) {
      $taxonomy_vocabulary = $this
        ->entityTypeManager()
        ->getStorage('taxonomy_vocabulary')
        ->load($term
        ->getVocabularyId());
      if ($taxonomy_vocabulary) {
        $terms = TermReferenceFancytree::loadTerms($taxonomy_vocabulary, $parent);
        $list = TermReferenceFancytree::getNestedListJsonArray($terms, []);
      }
    }
  }
  return new JsonResponse($list);
}