function TableDragExampleNestedForm::getData
Same name in other branches
- 3.x modules/tabledrag_example/src/Form/TableDragExampleNestedForm.php \Drupal\tabledrag_example\Form\TableDragExampleNestedForm::getData()
Retrieves the tree structure from db and 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 TableDragExampleNestedForm::getData()
- TableDragExampleNestedForm::buildForm in modules/
tabledrag_example/ src/ Form/ TableDragExampleNestedForm.php - Build the parent-child example form.
File
-
modules/
tabledrag_example/ src/ Form/ TableDragExampleNestedForm.php, line 248
Class
- TableDragExampleNestedForm
- Table drag example nested 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', '=')
->condition('id', 11, '<')
->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;
}