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

▾ 4 functions call menu_tree_output()

book_block_view in modules/book/book.module
Implement hook_block_view().
book_children in modules/book/book.module
Format the menu links for the child pages of the current page.
menu_tree in includes/menu.inc
Render a menu tree based on the current path.
menu_tree_output in includes/menu.inc
Returns a rendered menu tree.

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
 
 

All source code and documentation on this site is released under the terms of the GNU General Public License, version 2 and later. Drupal is a registered trademark of Dries Buytaert.