7.x menu.inc menu_build_tree($menu_name, array $parameters = array())

Builds a menu tree, translates links, and checks access.


$menu_name: The name of the menu.

$parameters: (optional) An associative array of build parameters. Possible keys:

  • expanded: An array of parent link ids to return only menu links that are children of one of the plids in this list. If empty, the whole menu tree is built, unless 'only_active_trail' is TRUE.
  • active_trail: An array of mlids, representing the coordinates of the currently active menu link.
  • only_active_trail: Whether to only return links that are in the active trail. This option is ignored, if 'expanded' is non-empty. Internally used for breadcrumbs.
  • min_depth: The minimum depth of menu links in the resulting tree. Defaults to 1, which is the default to build a whole tree for a menu (excluding menu container itself).
  • max_depth: The maximum depth of menu links in the resulting tree.
  • conditions: An associative array of custom database select query condition key/value pairs; see _menu_build_tree() for the actual query.

Return value

A fully built menu tree.

Related topics

3 calls to menu_build_tree()
menu_tree_all_data in includes/menu.inc
Gets the data structure representing a named menu tree.
menu_tree_page_data in includes/menu.inc
Gets the data structure for a named menu tree, based on the current page.
toolbar_get_menu_tree in modules/toolbar/toolbar.module
Gets only the top level items below the 'admin' path.


includes/menu.inc, line 1361
API for the Drupal menu system.


function menu_build_tree($menu_name, array $parameters = array()) {

  // Build the menu tree.
  $data = _menu_build_tree($menu_name, $parameters);

  // Check access for the current user to each item in the tree.
  menu_tree_check_access($data['tree'], $data['node_links']);
  return $data['tree'];


abhishek-sinha’s picture

I am using drupal7 or drupal 7.9 version and i have created some menus
and some sub menus but the problem is display the submenu. menu is display on the front end but the submenu is not being display on the front end.

Please resolve my problem because i have tried more time but unsuccessful

FTHEO’s picture

Would you please show me an example of "Menu and Submenu codes" with all are necessary to get them activily displayed in all sorts of web browsers?

kumkum29’s picture


I use this code to genereate a menu from a specific parent:

$path = 'node/'. $item_id;
$parent = menu_link_get_preferred($path);
$parameters = array(
	'active_trail' => array($parent['plid']),
	'only_active_trail' => FALSE,
	'min_depth' => $parent['depth']+1,
	'max_depth' => $parent['depth']+1,
	'conditions' => array('plid' => $parent['mlid']),
$children = menu_build_tree($parent['menu_name'], $parameters);
$menu_render = menu_tree_output($children);		
print drupal_render($menu_render);

But I get only one level of the menu.
How can I get the children menu-items?


chrisrockwell’s picture

@kumkum29 - change max_depth to get what you want.

pieterdt’s picture

changing max_depth will not help, it limits how deep the tree can be built, but given that there is a condition on plid, you only have the children to be inserted in the tree. I'm looking for the same thing, without luck. Based on the parameters that can be used, I cannot come up with something that returns all children up to a certain level for a certain menu item.

Anthony Robertson’s picture

I found the comments here particularly helpful but could not figure out how to add a wrapper div to the output of menu_tree_output(). I don't seem to get any results by trying to add #attributes, but I am able to provide a #prefix and #suffix to wrap the menu in a div like this:

$parent = menu_link_get_preferred("node/".$nid, 'my-special-menu');

$parameters = array(
... // your parameters may vary

$children = menu_build_tree($parent['menu_name'], $parameters);
$menu_tree = menu_tree_output($children);

$menu_tree["#prefix"] = "<div class='menu_wrapper_class'>";
$menu_tree["#suffix"] = "</div>";

I place $menu_tree into a page region using hook_preprocess_page() - and it renders with a nice wrapper div which helps me style it.

Hope this helps someone!

ashley_i72’s picture

Adding the "expanded" parameter helped to return the child elements. It says it accepts an array of plids to access the children, otherwise displays the whole menu.

I added the following in the $parameters array:
'expanded' => array($parent['mlid']),

I also made "only_active_trail" => true because I only want the children for this page.

Hope this helps someone, know this topic is a bit old.