public function TableDragExampleRootLeafForm::getData in Examples for Developers 3.x
Same name and namespace in other branches
- 8 tabledrag_example/src/Form/TableDragExampleRootLeafForm.php \Drupal\tabledrag_example\Form\TableDragExampleRootLeafForm::getData()
Retrieves the tree structure from database, sorts by parent/child/weight.
The sorting should result in children items immediately following their parent items, with items at the same level of the hierarchy sorted by weight.
The approach used here may be considered too database-intensive. Optimization of the approach is left as an exercise for the reader. :)
Return value
array An associative array storing our ordered tree structure.
1 call to TableDragExampleRootLeafForm::getData()
- TableDragExampleRootLeafForm::buildForm in modules/
tabledrag_example/ src/ Form/ TableDragExampleRootLeafForm.php - Build the parent-child example form.
File
- modules/
tabledrag_example/ src/ Form/ TableDragExampleRootLeafForm.php, line 282
Class
- TableDragExampleRootLeafForm
- Table drag example root leaf form.
Namespace
Drupal\tabledrag_example\FormCode
public function getData() {
// Get all 'root node' items (items with no parents), sorted by weight.
$root_items = $this->database
->select('tabledrag_example', 't')
->fields('t')
->condition('pid', '0', '=')
->orderBy('weight')
->execute()
->fetchAll();
// Initialize a variable to store our ordered tree structure.
$tree = [];
// Depth will be incremented in our getTree()
// function for the first parent item, so we start it at -1.
$depth = -1;
// Loop through the root item, and add their trees to the array.
foreach ($root_items as $root_item) {
$this
->getTree($root_item, $tree, $depth);
}
return $tree;
}