You are here

public function TaxonomyManagerTree::getPartialTree in Taxonomy Manager 8

Same name and namespace in other branches
  1. 2.0.x src/Element/TaxonomyManagerTree.php \Drupal\taxonomy_manager\Element\TaxonomyManagerTree::getPartialTree()

Returns partial tree for a given path.

1 call to TaxonomyManagerTree::getPartialTree()
TaxonomyManagerTree::getFirstPath in src/Element/TaxonomyManagerTree.php
Helper function to calculate path.

File

src/Element/TaxonomyManagerTree.php, line 241

Class

TaxonomyManagerTree
Taxonomy Manager Tree Form Element.

Namespace

Drupal\taxonomy_manager\Element

Code

public function getPartialTree($path, $depth = 0) {
  $tree = [];
  $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 = [
      0 => $parent->tid,
    ];
    $tree[] = [
      '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;
}