You are here

protected function MediaDirectoriesController::buildTree in Media Directories 8

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

Populates a tree array given a taxonomy term tree object.

Parameters

array $tree: An array representing the tree.

object $object: A taxonomy term.

string $vocabulary: The vocabulary id.

1 call to MediaDirectoriesController::buildTree()
MediaDirectoriesController::directoryTree in modules/media_directories_ui/src/Controller/MediaDirectoriesController.php
Return directory tree as JSON.

File

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

Class

MediaDirectoriesController
Main controller class.

Namespace

Drupal\media_directories_ui\Controller

Code

protected function buildTree(array &$tree, $object, $vocabulary) {
  if ($object->depth !== 0) {
    return;
  }
  $tree[$object->tid] = $object;
  $tree[$object->tid]->children = [];
  $tree[$object->tid]->text = $object->name;
  if ($this->contentTranslationEnabled) {
    $current_language = $this->languageManager
      ->getCurrentLanguage();
    $term = Term::load($object->tid);
    if ($term && $term
      ->hasTranslation($current_language
      ->getId())) {
      $term = $term
        ->getTranslation($current_language
        ->getId());
      $tree[$object->tid]->text = $term
        ->label();
    }
  }
  $tree[$object->tid]->a_attr = [
    'data-tid' => $object->tid,
  ];
  $tree[$object->tid]->id = 'dir-' . $object->tid;
  $object_children =& $tree[$object->tid]->children;
  $children = $this->termStorage
    ->loadChildren($object->tid);
  if (!$children) {
    return;
  }
  $child_tree_objects = $this->termStorage
    ->loadTree($vocabulary, $object->tid);
  foreach ($children as $child) {
    foreach ($child_tree_objects as $child_tree_object) {
      if ($child_tree_object->tid === $child
        ->id()) {
        $this
          ->buildTree($object_children, $child_tree_object, $vocabulary);
      }
    }
  }
  $tree[$object->tid]->children = array_values($tree[$object->tid]->children);
}