You are here

public static function TaxonomyManagerTree::getNestedList in Taxonomy Manager 8

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

Helper function that transforms a flat taxonomy tree in a nested array.

File

src/Element/TaxonomyManagerTree.php, line 141

Class

TaxonomyManagerTree
Taxonomy Manager Tree Form Element.

Namespace

Drupal\taxonomy_manager\Element

Code

public static function getNestedList($tree = [], $max_depth = NULL, $parent = 0, $parents_index = [], $depth = 0) {
  foreach ($tree as $term) {
    foreach ($term->parents as $term_parent) {
      if ($term_parent == $parent) {
        $return[$term
          ->id()] = $term;
      }
      else {
        $parents_index[$term_parent][$term
          ->id()] = $term;
      }
    }
  }
  foreach ($return as &$term) {
    if (isset($parents_index[$term
      ->id()]) && (is_null($max_depth) || $depth < $max_depth)) {
      $term->children = TaxonomyManagerTree::getNestedList($parents_index[$term
        ->id()], $max_depth, $term
        ->id(), $parents_index, $depth + 1);
    }
  }
  return $return;
}