order_tree_nodes()

Description

Order array of tree nodes into logical order - Each parent followed by its child nodes, all following order_by

Parameters

ColumnTypeDefaultDescription
$nodes array Array of detailed nodes
$order_by_translated_name false

Return

array Full nodes in order

Location

include/node_functions.php lines 2472 to 2541

Definition

 
function order_tree_nodes($nodes$order_by_translated_name false)
{
    if (
count($nodes) == 0) {
        return [];
    }

    if (
$order_by_translated_name) {
        
$sort 'node_translated_name_comparator';
    } else {
        
$sort 'node_orderby_comparator';
    }
    
// Find parent nodes first
    
$parents array_column($nodes"parent");
    
$toplevels min($parents) > $parents : [0];
    
$orderednodes array_values(array_filter($nodes, function ($node) use ($toplevels) {
        return 
in_array((int)$node["parent"], $toplevels);
    }));

    if (!
$order_by_translated_name) {
        
usort($orderednodes$sort);
    }

    for (
$n 0$n count($orderednodes); $n++) {
        
$orderednodes[$n]["path"] = $orderednodes[$n]["name"];
        if (!isset(
$orderednodes[$n]["translated_name"]) || is_i18n_language_string($orderednodes[$n]["translated_name"])) {
            
// Where translated_path is in i18n format, add_sql_node_language() couldn't find a match with the current language. Translate it to get default language.
            
$orderednodes[$n]["translated_path"] = i18n_get_translated($orderednodes[$n]["name"]);
        } else {
            
$orderednodes[$n]["translated_path"] = $orderednodes[$n]["translated_name"];
        }
    }

    
// Find child nodes
    
$parents_processed = [];
    while (
count($nodes) > 0) {
        
// Loop to find children
        
for ($n 0$n count($orderednodes); $n++) {
            if (!
in_array($orderednodes[$n]["ref"], $parents_processed)) {
                
// Add the children of this node with the the path added (relative to paremnt)
                
$children array_filter($nodes, function ($node) use ($orderednodes$n) {
                    return (int)
$node["parent"] == $orderednodes[$n]["ref"];
                });
                
// Set order
                
uasort($children$sort);
                
$children array_values($children);
                for (
$c 0$c count($children); $c++) {
                    
$children[$c]["path"] = $orderednodes[$n]["path"] . "/" .  $children[$c]["name"];
                    if (!isset(
$children[$c]["translated_name"]) || is_i18n_language_string($children[$c]["translated_name"])) {
                        
// Where translated_path is in i18n format, add_sql_node_language() couldn't find a match with the current language. Translate it to get default language.
                        
$children[$c]["translated_path"] = $orderednodes[$n]["translated_path"] . "/" .  i18n_get_translated($children[$c]["name"]);
                    } else {
                        
$children[$c]["translated_path"] = $orderednodes[$n]["translated_path"] . "/" .  ($children[$c]["translated_name"]);
                    }
                    
// Insert the child after the parent and any nodes with a lower order_by value
                    
array_splice($orderednodes$n $c0, [$children[$c]]);
                    
// Remove child from $treenodes
                    
$pos array_search($children[$c]["ref"], array_column($nodes"ref"));
                    unset(
$nodes[$pos]);
                    
$nodes array_values($nodes);
                }
                
$parents_processed[] = $orderednodes[$n]["ref"];
            } else {
                
$pos array_search($orderednodes[$n]["ref"], array_column($nodes"ref"));
                unset(
$nodes[$pos]);
            }
        }
        
$nodes array_values($nodes);
    }
    return 
$orderednodes;
}

This article was last updated 1st June 2025 21:35 Europe/London time based on the source file dated 9th May 2025 16:15 Europe/London time.