function tabledrag_example_get_tree

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

Related topics

1 call to tabledrag_example_get_tree()
tabledrag_example_parent_get_data in tabledrag_example/tabledrag_example_parent_form.inc
Retrives the tree structure from database, and sorts by parent/child/weight.

File

tabledrag_example/tabledrag_example_parent_form.inc, line 310

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--;
}