You are here

public function TableDragExampleRootLeafForm::getTree in Examples for Developers 8

Same name and namespace in other branches
  1. 3.x modules/tabledrag_example/src/Form/TableDragExampleRootLeafForm.php \Drupal\tabledrag_example\Form\TableDragExampleRootLeafForm::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 TableDragExampleRootLeafForm::getTree()
TableDragExampleRootLeafForm::getData in tabledrag_example/src/Form/TableDragExampleRootLeafForm.php
Retrieves the tree structure from database, sorts by parent/child/weight.

File

tabledrag_example/src/Form/TableDragExampleRootLeafForm.php, line 316

Class

TableDragExampleRootLeafForm
Table drag example root leaf 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, '=')
    ->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--;
}