Collections functions
General functions
Node functions
Render functions
Theme permission functions
User functions
Resource functions

compute_node_branch_path()

Description

Get to the root of the branch starting from a node.

IMPORTANT: the term nodes here is generic, it refers to a tree node structure containing at least ref and parent

Parameters

ColumnTypeDefaultDescription
$nodes array List of nodes to search through (MUST contain elements with at least the "ref" index)
$id integer Node ref we compute the branch path for

Return

array Branch path structure starting from root to the searched node

Location

include/node_functions.php lines 2278 to 2333

Definition

 
function compute_node_branch_path(array $nodesint $id)
    {
    if(empty(
$nodes))
        {
        return array();
        }

    global 
$NODE_BRANCH_PATHS_CACHE;
    
$NODE_BRANCH_PATHS_CACHE = (!is_null($NODE_BRANCH_PATHS_CACHE) && is_array($NODE_BRANCH_PATHS_CACHE) ? $NODE_BRANCH_PATHS_CACHE : array());
    
// create a unique ID for this list of nodes since these can be used for anything
    
$nodes_list_id md5(json_encode($nodes));

    if(isset(
$NODE_BRANCH_PATHS_CACHE[$nodes_list_id][$id]))
        {
        return 
$NODE_BRANCH_PATHS_CACHE[$nodes_list_id][$id];
        }

    
$nodes_ref_list array_column($nodes'ref');

    
$found_node_index array_search($id$nodes_ref_list);
    if(
$found_node_index === false)
        {
        return array();
        }

    
$node $nodes[$found_node_index];
    
$node_parent = (isset($node["parent"]) && $node["parent"] > ? (int) $node["parent"] : null);

    
$path = array($node);
    while(!
is_null($node_parent))
        {
        
// Parent node path is already known (cached), use it instead of recalculating it
        
if(isset($NODE_BRANCH_PATHS_CACHE[$nodes_list_id][$node_parent]))
            {
            
$parent_branch_reversed array_reverse($NODE_BRANCH_PATHS_CACHE[$nodes_list_id][$node_parent]);
            
$path array_merge($path$parent_branch_reversed);
            break;
            }

        
$found_node_index array_search($node_parent$nodes_ref_list);
        if(
$found_node_index === false)
            {
            break;
            }

        
$node $nodes[$found_node_index];
        
$node_parent = (isset($node["parent"]) && $node["parent"] > ? (int) $node["parent"] : null);

        
$path[] = $node;
        }

    
$path_reverse array_reverse($path);
    
$NODE_BRANCH_PATHS_CACHE[$nodes_list_id][$id] = $path_reverse;

    return 
$path_reverse;
    }

This article was last updated 19th March 2024 10:05 Europe/London time based on the source file dated 15th March 2024 17:00 Europe/London time.