1 tabledrag_example_parent_form.inc tabledrag_example_get_tree($parentitem, &$itemtree = array(), &$depth = 0)

Recursively adds to the $itemtree array, ordered by parent/child/weight.

Related topics

File

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

Code

function tabledrag_example_get_tree($parentitem, &$itemtree = array(), &$depth = 0) {
  // Increase our $depth value by one.
  $depth++;
  // Set the current tree 'depth' for this item, used to calculate indentation.
  $parentitem->depth = $depth;
  // Add the parent item to the tree.
  $itemtree[$parentitem->id] = $parentitem;
  // Retrieve each of the children belonging to this parent.
  $children = db_query('SELECT id, name, description, weight, pid
                      FROM {tabledrag_example}
                      WHERE (pid = :pid)
                      ORDER BY weight ASC', 
  array(':pid' => $parentitem->id));
  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($itemtree))) {
      // Add this child's tree to the $itemtree array.
      tabledrag_example_get_tree($child, $itemtree, $depth);
    }
  }
  // Finished processing this tree branch.  Decrease our $depth value by one
  // to represent moving to the next branch.
  $depth--;
}