menu_tree_output
- Versions
- 6 – 7
menu_tree_output($tree)
Returns a rendered menu tree.
The menu item's LI element is given one of the following classes:
- expanded: The menu item is showing its submenu.
- collapsed: The menu item has a submenu which is not shown.
- leaf: The menu item has no submenu.
Parameters
$tree A data structure representing the tree as returned from menu_tree_data.
Return value
A structured array to be rendered by drupal_render().
Related topics
Code
includes/menu.inc, line 882
<?php
function menu_tree_output($tree) {
$build = array();
$items = array();
// Pull out just the menu links we are going to render so that we
// get an accurate count for the first/last classes.
foreach ($tree as $data) {
if (!$data['link']['hidden']) {
$items[] = $data;
}
}
$num_items = count($items);
foreach ($items as $i => $data) {
$class = array();
if ($i == 0) {
$class[] = 'first';
}
if ($i == $num_items - 1) {
$class[] = 'last';
}
// Set a class if the link has children.
if ($data['below']) {
$class[] = 'expanded';
}
elseif ($data['link']['has_children']) {
$class[] = 'collapsed';
}
else {
$class[] = 'leaf';
}
// Set a class if the link is in the active trail.
if ($data['link']['in_active_trail']) {
$class[] = 'active-trail';
$data['localized_options']['attributes']['class'][] = 'active-trail';
}
$element['#theme'] = 'menu_link';
$element['#attributes']['class'] = $class;
$element['#title'] = $data['link']['title'];
$element['#href'] = $data['link']['href'];
$element['#localized_options'] = !empty($data['localized_options']) ? $data['localized_options'] : array();
$element['#below'] = $data['below'] ? menu_tree_output($data['below']) : $data['below'];
$element['#original_link'] = $data['link'];
// Index using the link's unique mlid.
$build[$data['link']['mlid']] = $element;
}
if ($build) {
// Make sure drupal_render() does not re-order the links.
$build['#sorted'] = TRUE;
// Add the theme wrapper for outer markup.
$build['#theme_wrappers'][] = 'menu_tree';
}
return $build;
}
?>Login or register to post comments 