You are here

public static function TermReferenceFancytree::getNestedList in Term Reference Fancytree 8.2

Same name and namespace in other branches
  1. 8 src/Element/TermReferenceFancytree.php \Drupal\term_reference_fancytree\Element\TermReferenceFancytree::getNestedList()

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

File

src/Element/TermReferenceFancytree.php, line 231

Class

TermReferenceFancytree
Term Reference Tree Form Element.

Namespace

Drupal\term_reference_fancytree\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 = TermReferenceFancytree::getNestedList($parents_index[$term
        ->id()], $max_depth, $term
        ->id(), $parents_index, $depth + 1);
    }
  }
  return $return;
}