You are now viewing the documentation of eZ Publish legacy (4.x series and the legacy "stack" in 5.x). For 5.x versions this documentation must be complemented with the latest, more current documentation available at confluence.ez.no. Happy learning!

tree

Summary

Fetches the children of a node recursively.

Usage

fetch( 'content', 'tree',
       hash( 'parent_node_id',     parent_node_id,
           [ 'sort_by',                    sort_by,                   ]
           [ 'offset',                     offset,                    ]
           [ 'limit',                      limit,                     ]
           [ 'attribute_filter',           attribute_filter,          ]
           [ 'extended_attribute_filter',  extended_attribute_filter, ]
           [ 'class_filter_type',          class_filter_type,         ]
           [ 'class_filter_array',         class_filter_array,        ]
           [ 'only_translated',            only_translated,           ]
           [ 'language',                   language,                  ]
           [ 'main_node_only',             main_node_only,            ]
           [ 'as_object',                  as_object,                 ]
           [ 'depth',                      depth,                     ]
           [ 'depth_operator',             depth_operator             ]
           [ 'limitation',                 limitation                 ]
           [ 'ignore_visibility',          ignore_visibility          ] ) )

Parameters

NameTypeDescriptionRequired
parent_node_id integer The ID number of the parent node. Yes.
sort_by array The sorting mechanism that should be used. No.
offset integer The offset to start at. No.
limit integer The maximum number of nodes that should be fetched. No.
attribute_filter mixed Filter logic for attribute level filtering. No.
extended_attribute_filter mixed The extended attribute level filter logic. No.
class_filter_type string The type of class filtering (include/exclude). No.
class_filter_array array The type of nodes that should be filtered. No.
only_translated boolean Translation filtering (on/off). No.
language string The language that should be filtered. No.
main_node_only boolean Type of nodes that should be fetched (all or main nodes only). No.
as_object boolean If TRUE (or omitted), an array of "ezcontentobjecttreenode" objects will be fetched. Otherwise, an array of arrays will be returned. No.
depth integer The maximum level of depth that should be explored. No.
depth_operator string The logic to use when checking the depth. No.
limitation array Limitation array (emtpy array = access override). No.
ignore_visibility boolean Makes it possible to fetch hidden nodes. No.

Description

The "tree" function is very similar to the "list" fetch function. The only difference is that the tree function fetches child nodes recursively. The recursion depth can be controlled by the "depth" and "depth_operator" parameters. The rest of the parameters behave exactly in the same way as they do for the "list" function. Please refer to the documentation page of the "list" function for a complete explanation of the parameters.

Depth

The depth parameter can be used to specify the level of depth (within the branch) that the function should explore when it is running. If the depth is set to one, this function will simply act as the list function. If the depth is greater than one, the function will fetch nodes further down in the branch. The default value is unlimited.

Depth operator

The depth operator can be set to either "lt", "eq" or "gt" - meaning "less than", "equal to" and "greather than". For example, if it is set to 'eq', only nodes with the depth that was specified using the depth parameter will be fetched.

Examples

Example 1

{def $nodes=fetch( 'content', 'tree',
                   hash( 'parent_node_id', 42 ) )}
 
{foreach $nodes as $node}
    {$node.name|wash} <br />
{/foreach}

This example demonstrates how to fetch all the nodes that are under node number 42 recursively (all children, grand-children, etc. will be fetched). The names of the nodes are displayed.

Balazs Halasy (06/02/2004 1:15 pm)

André R. (18/03/2011 4:19 pm)

Balazs Halasy, Svitlana Shatokhina, Geir Arne Waaler, André R.


Comments

  • tree is not a tree but a list

    Be aware that 'tree' doesn't return a tree, matrix of array of arrays but a list.
    Making a sort on a 'tree' totaly destroys the tree structure and sort all the listed objects without taking in count there depth or parent node.If you wan't to search by depth and parent, it seems that you can't. No recursive function to step easily through this not-tree-but-list.
    • Re: tree is not a tree but a list

      Each object is a tree.
      I used depth=1 and parse childs.
      Example :
      {def $tree_node = fetch('content', 'tree', hash( 'parent_node_id', $pagedata.root_node, 'depth', 1 ) ) }
      <ul>
      {foreach $tree_node as $key => $item}
      		<li{cond($key|eq(0), ' class="firstli"')}><a href={$item.url_alias|ezurl(no)}>{$item.name}</a>
      			<ul>
      	{foreach $item.children as $subkey => $subitem}
      			<li{cond($subkey|eq(0), ' class="firstli"')}><a href={$subitem.url_alias|ezurl(no)}>{$subitem.name}</a></li>
      	{/foreach}
      			</ul>
      		</li>
      {/foreach}
      </ul>