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

get_parent_nodes()

Description

Get all the parent nodes of the given node, all the way back to the top of the node tree.

Parameters

ColumnTypeDefaultDescription
$noderef integer The child node ID
$detailed bool false Return all node data? false by default
$include_child bool false Include the passed node in the returned array (easier for resolving tree nodes to paths)? false by default

Return

array Array of the parent node IDs

Location

include/node_functions.php lines 1758 to 1820

Definition

 
function get_parent_nodes(int $noderef,bool $detailed false$include_child=false)
    {
    
// Get all parents. Query varies according to MySQL cte support
    
$mysql_version ps_query('SELECT LEFT(VERSION(), 3) AS ver');
    if(
version_compare($mysql_version[0]['ver'], '8.0''>=')) 
        {
        
$colsa $detailed "ref, name, parent, resource_type_field, order_by, `active`" "ref, name, parent";
        
$colsb $detailed "n.ref, n.name, n.parent, n.resource_type_field, n.order_by, n.`active`" "n.ref, n.name, n.parent";
        
$parent_nodes ps_query("
            WITH RECURSIVE cte(
$colsa,level) AS
                    (
                    SELECT 
$colsa,
                           1 AS level
                      FROM node
                     WHERE ref= ?
                 UNION ALL
                    SELECT 
$colsb,
                           level+1 AS LEVEL
                      FROM  node n
                INNER JOIN  cte
                        ON  n.ref = cte.parent
                    )
            SELECT 
$colsa
              FROM cte
          ORDER BY level ASC;"
,
        [
'i'$noderef]);
        }
    else
        {
        
$colsa $detailed columns_in("node","N2") : "ref, name";
        
$parent_nodes ps_query("
        SELECT  
$colsa
        FROM  (SELECT @r AS p_ref,
                (SELECT @r := parent FROM node WHERE ref = p_ref) AS parent,
                @l := @l + 1 AS lvl
        FROM  (SELECT @r := ?, @l := 0) vars,
                node c
        WHERE  @r <> 0) N1
        JOIN  node N2
            ON  N1.p_ref = N2.ref
        ORDER BY  N1.lvl ASC"
,
            [
'i'$noderef]);
        }

    if(!
$include_child)
        {
        
$parent_nodes array_values(array_filter($parent_nodes,function($node) use ($noderef) {return $node["ref"] != $noderef;}));
        }

    if(!
$detailed)
        {
        
$parent_nodes array_column($parent_nodes,"name""ref");
        }
    else
        {
        for(
$n=0;$n<count($parent_nodes);$n++)
            {
            
$parent_nodes[$n]["translated_name"] = i18n_get_translated($parent_nodes[$n]["name"]);
            }
        }

    return 
$parent_nodes;
    }

This article was last updated 24th May 2024 10:05 Europe/London time based on the source file dated 14th May 2024 18:25 Europe/London time.