1 tabledrag_example_parent_form.inc tabledrag_example_parent_get_data()

Retrives the tree structure from database, 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. :)

Related topics

File

modules/examples/tabledrag_example/tabledrag_example_parent_form.inc, line 287
Example demonstrating a parent/child tabledrag form

Code

function tabledrag_example_parent_get_data() {
  // Get all 'root node' items (items with no parents), sorted by weight.
  $rootnodes = db_query('SELECT id, name, description, weight, pid
                       FROM {tabledrag_example}
                       WHERE (pid = 0)
                       ORDER BY weight ASC');
  // Initialize a variable to store our ordered tree structure.
  $itemtree = array();
  // Depth will be incremented in our _get_tree() function for the first
  // parent item, so we start it at -1.
  $depth = -1;
  // Loop through the root nodes, and add their trees to the array.
  foreach ($rootnodes as $parent) {
    tabledrag_example_get_tree($parent, $itemtree, $depth);
  }
  return $itemtree;
}