Caution: This documentation is for eZ Publish legacy, from version 3.x to 5.x.
For 5.x documentation covering Platform see eZ Documentation Center, for difference between legacy and Platform see 5.x Architecture overview.

treemenu

Summary

Fetches a subtree of nodes for the purpose of menu generation.

Usage

treemenu( path 
          [, node_id            ]
          [, class_filter       ]
          [, depth_skip         ]
          [, max_level          ] 
          [, is_selected_method ] 
          [, indentation_level  ]
          [, language           ] )

Parameters

NameTypeDescriptionRequired
path array An array of the path ($module_result.path). Yes.
node_id integer DEPRECATED (The node ID number, i.e. the root of the subtree.) No.
class_filter array An array of classes that should be filtered. No.
depth_skip integer Number of levels that should be skipped. No.
max_level integer The max depth that should be explored. (2 by default) No.
is_selected_method string Sets whether "is_selected=TRUE" should be assigned to the parents of the current node as well. No.
indentation_level integer The size of increment to use when calculating the indentation of the menu entries (15 by default). No.
language string/array The language(s) to use when fetching the nodes. Use either a string, e.g. 'nor-NO', or an array of strings, e.g. array( 'nor-NO', 'eng-GB' ). No.

Returns

A complex structure that can be used to build a menu (see below).

Description

This operator fetches a subtree of nodes and returns a complex structure (an array of hashes) that can be used to generate a menu. Note that the optional "node_id" parameter is deprecated and no longer used (its value is ignored by the system, but the parameter itself is not removed because of backwards compatibility reasons). The root of the subtree is now determined by the "path" parameter. If the provided path array points to a node, then a subtree of this node will be fetched. (Use "$module_result.path" to pass the path which leads to the page that is currently being viewed.) If the "path" parameter does not point to a node in the content tree, then a subtree of the "Content" top level node will be fetched.

If the optional "class_filter" parameter is omitted, all nodes will be fetched without filtering. If an empty array is passed, then only folder nodes (class ID = 1) will be fetched.

The optional "language" parameter makes it possible to specify which languages to use when fetching the nodes. The languages will be prioritized according to the array (this overrides the language settings of the siteaccess).

The default value of the "is_selected_method" parameter is "tree". This value determines that the current node and its parent nodes will be considered as selected (is_selected=TRUE). If this parameter is set to "node", only the current node will be considered as selected.

The following table shows the hash-structure for each element in the array that will be returned.

Key

Type

Description

id

integer

The ID of the node.

level

integer

The depth of the node.

url_alias

string

The URL alias of the node.

url

string

The system URL of the node.

text

string

The name of the node.

is_selected

boolean

TRUE if the node is currently being viewed/selected, FALSE otherwise.

If the "path" parameter points to a node in the content tree, then the following additional keys will be included in the hash structure:

Key

Type

Description

data_map

array

The attributes (as ezcontentobjectattribute objects) of the actual content object encapsulated by the node.

class_name

string

The name of the class which the object encapsulated by the node is an instance of, for example "Folder".

is_main_node

boolean

TRUE if the node is a main node, FALSE otherwise.

has_children

boolean

TRUE if the node has children, FALSE otherwise.

indent

integer

The indentation of the menu entries. This value is calculated according to the depth of the node using "indentation_level" as increment. A menu item of the level N will have indent=(N-1)*indentation_level.

Examples

Example 1 (explanatory)

Let's use a small site with the following content structure (see the screenshot) for the purpose of demonstration

The content tree

The content tree

and insert the following code into "pagelayout.tpl":

{def $mainMenu=treemenu( $module_result.path ) }
{foreach $mainMenu as $menu}
{$menu.level} - {$menu.text}<br />
{/foreach}
{undef $mainMenu}

 

If the "Weblog" node is being viewed, then the following output will be produced:

0 - Weblog
1 - July, 29
1 - July, 14
1 - June, 25
0 - Galleries
0 - Products

 

If the "Blue flower" node is being viewed, then the following output will be produced:

0 - Weblog
0 - Galleries
1 - Misc flowers
1 - Landscape
0 - Products

 

Since the "max_level" parameter is omitted, only two levels are explored. To set the "max_level" parameter to 3, change the first line of the previous code fragment in the following way:

{def $mainMenu=treemenu( $module_result.path, , , , 3 ) }

 

This will produce the following output for the "Blue flower" node:

0 - Weblog
0 - Galleries
1 - Misc flowers
2 - Red flower
2 - Blue flower
1 - Landscape
0 - Products

 

To skip the first level, set the "depth_skip" parameter to 1 by changing the first line of the code fragment as shown below:

{def $mainMenu=treemenu( $module_result.path, , , 1, 3 ) }

 

If the "Blue flower" node is being viewed, then the following output will be produced:

0 - Misc flowers
1 - Red flower
1 - Blue flower
0 - Landscape

 

Now, let's use another code fragment in order to see which items are selected:

{def $mainMenu=treemenu( $module_result.path, , , , 3 )}
{foreach $mainMenu as $menu}
    {if $menu.is_selected}
        {$menu.level} - {$menu.text} (selected) <br />
    {else}
        {$menu.level} - {$menu.text} <br />
    {/if}
{/foreach}
{undef $mainMenu}

 

Since the "is_selected_method" parameter is omitted, the "tree" mode will be used and the following output will be produced for the "Blue flower" node:

0 - Weblog
0 - Galleries (selected)
1 - Misc flowers (selected)
2 - Red flower
2 - Blue flower (selected)
1 - Landscape
0 - Products

 

To set the "is_selected_method" parameter to "node", replace the first line of the last code fragment by the following line:

{def $mainMenu=treemenu( $module_result.path, , , , 3, 'node' )}

 

If the "Blue flower" node is being viewed, then the following output will be produced:

0 - Weblog
0 - Galleries
1 - Misc flowers
2 - Red flower
2 - Blue flower (selected)
1 - Landscape
0 - Products

 

Example 2

These examples are from pagelayout.tpl.

Make a menu of folder and info_page classes. Skip the first level and maximum go to depth 6.

<ul>
{def $mainMenu=treemenu( $module_result.path, ,
                                    array('folder','info_page'), 1, 6 )}
 
{foreach $mainMenu as $menu}
    <li class="level_{$menu.level}">
 
    {if $menu.is_selected}
        <div class="selected">
        <a href={$menu.url_alias|ezurl}>{$menu.text}</a>
        </div>
    {else}
        <a href={$menu.url_alias|ezurl}>{$menu.text}</a>
    {/if}
 
    </li>
{/foreach}
</ul>

Make a menu which shows the sub menu items when clicked on the parent menu item. Notice that only the objectclass ids: 1, 9, and 17 are visible.

{def $docs=treemenu( $module_result.path, ,
                                    array(1, 9, 17), 0, 4)}
{def $depth=1 $last=0}
 
{foreach $docs as $menu}
    {if and($last | ne(0), $last.level|gt($menu.level))}
        </ul>
        </li>
    {/if}
 
    <li>
 
    {if and($last | ne(0), $last.level| lt($menu.level))}
        <ul>
        <li>
    {/if}
 
    <a {$menu.is_selected|choose('','class="selected"')}
        href={$menu.url_alias|ezurl}>{$menu.text|shorten(25)}</a>
    </li>
 
    {set last=$menu}
{/foreach}
 
{while $depth |gt(1)}
    </li>
    </ul>
    {set depth=$depth|sub(1)}
{/while}

Balazs Halasy (05/02/2004 10:51 am)

Julia Shymova (29/11/2007 9:50 am)

Svitlana Shatokhina, Julia Shymova


Comments

  • Bug in example

    Last example has a little bug when you select the last entry in a structure that has more than two levels of depth. The right code is:

    {def $depth=1 $last=0 $lastlevel=0}
            <ol>
    {foreach $mainMenu as $menu}
        {set lastlevel=$last.level}
        {while and($lastlevel|ne(0), $lastlevel|gt($menu.level))}
            </ol>
                    </li>
             {set lastlevel=$lastlevel|sub(1)}
        {/if}
     
                    <li>
     
        {if and($last | ne(0), $last.level|lt($menu.level))}
            <ol>
                    <li{$menu.is_selected|choose('',' class="selected"')}>
        {/if}
     
                            <a {$menu.is_selected|choose('','class="selected"')}
                            href={$menu.url_alias|ezurl}>{$menu.text|shorten(25)}</a>
                    </li>
     
        {set last=$menu}
    {/foreach}
     
    {while $depth |gt(1)}
                    </li>
            </ol>
        {set depth=$depth|sub(1)}
    {/while}
     
            </ol>
    


    Enjoy :-)

    g