You are here

public function MediaDirectoriesController::directoryTree in Media Directories 8

Same name and namespace in other branches
  1. 3.x modules/media_directories_ui/src/Controller/MediaDirectoriesController.php \Drupal\media_directories_ui\Controller\MediaDirectoriesController::directoryTree()
  2. 2.x modules/media_directories_ui/src/Controller/MediaDirectoriesController.php \Drupal\media_directories_ui\Controller\MediaDirectoriesController::directoryTree()

Return directory tree as JSON.

Return value

\Symfony\Component\HttpFoundation\JsonResponse A JSON response.

Throws

\Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException

\Drupal\Component\Plugin\Exception\PluginNotFoundException

1 string reference to 'MediaDirectoriesController::directoryTree'
media_directories_ui.routing.yml in modules/media_directories_ui/media_directories_ui.routing.yml
modules/media_directories_ui/media_directories_ui.routing.yml

File

modules/media_directories_ui/src/Controller/MediaDirectoriesController.php, line 122

Class

MediaDirectoriesController
Main controller class.

Namespace

Drupal\media_directories_ui\Controller

Code

public function directoryTree() {
  $tree = [];
  $this->termStorage = $this
    ->entityTypeManager()
    ->getStorage('taxonomy_term');
  $terms = $this->termStorage
    ->loadTree($this->vocabularyId);
  foreach ($terms as $term) {
    $this
      ->buildTree($tree, $term, $this->vocabularyId);
  }
  $tree = [
    [
      'id' => 'dir-root',
      'text' => $this
        ->t('Root'),
      'state' => [
        'opened' => TRUE,
        'selected' => TRUE,
      ],
      'a_attr' => [
        'data-tid' => MEDIA_DIRECTORY_ROOT,
      ],
      'children' => array_values($tree),
    ],
  ];
  return new JsonResponse($tree);
}