function TableDragExampleNestedForm::getTree

Same name in other branches
  1. 3.x modules/tabledrag_example/src/Form/TableDragExampleNestedForm.php \Drupal\tabledrag_example\Form\TableDragExampleNestedForm::getTree()
  2. 4.0.x modules/tabledrag_example/src/Form/TableDragExampleNestedForm.php \Drupal\tabledrag_example\Form\TableDragExampleNestedForm::getTree()

Recursively adds $item to $item_tree, ordered by parent/child/weight.

Parameters

mixed $item: The item.

array $tree: The item tree.

int $depth: The depth of the item.

1 call to TableDragExampleNestedForm::getTree()
TableDragExampleNestedForm::getData in tabledrag_example/src/Form/TableDragExampleNestedForm.php
Retrieves the tree structure from db and sorts by parent/child/weight.

File

tabledrag_example/src/Form/TableDragExampleNestedForm.php, line 283

Class

TableDragExampleNestedForm
Table drag example nested form.

Namespace

Drupal\tabledrag_example\Form

Code

public function getTree($item, array &$tree = [], &$depth = 0) {
    // Increase our $depth value by one.
    $depth++;
    // Set the current tree 'depth' for this item, used to calculate
    // indentation.
    $item->depth = $depth;
    // Add the item to the tree.
    $tree[$item->id] = $item;
    // Retrieve each of the children belonging to this nested demo.
    $children = $this->database
        ->select('tabledrag_example', 't')
        ->fields('t')
        ->condition('pid', $item->id, '=')
        ->condition('id', 11, '<')
        ->orderBy('weight')
        ->execute()
        ->fetchAll();
    foreach ($children as $child) {
        // Make sure this child does not already exist in the tree, to
        // avoid loops.
        if (!in_array($child->id, array_keys($tree))) {
            // Add this child's tree to the $itemtree array.
            $this->getTree($child, $tree, $depth);
        }
    }
    // Finished processing this tree branch.  Decrease our $depth value by one
    // to represent moving to the next branch.
    $depth--;
}