function _menu_build_tree

Builds a menu tree.

This function may be used build the data for a menu tree only, for example to further massage the data manually before further processing happens. menu_tree_check_access() needs to be invoked afterwards.

See also


Related topics

1 call to _menu_build_tree()
menu_build_tree in drupal/includes/
Builds a menu tree, translates links, and checks access.
1 string reference to '_menu_build_tree'
menu_reset_static_cache in drupal/includes/
Resets the menu system static cache.


drupal/includes/, line 1378
API for the Drupal menu system.


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

  // Static cache of already built menu trees.
  $trees =& drupal_static(__FUNCTION__, array());

  // Build the cache id; sort parents to prevent duplicate storage and remove
  // default parameter values.
  if (isset($parameters['expanded'])) {
  $tree_cid = 'links:' . $menu_name . ':tree-data:' . $GLOBALS['language']->language . ':' . hash('sha256', serialize($parameters));

  // If we do not have this tree in the static cache, check {cache_menu}.
  if (!isset($trees[$tree_cid])) {
    $cache = cache_get($tree_cid, 'cache_menu');
    if ($cache && isset($cache->data)) {
      $trees[$tree_cid] = $cache->data;
  if (!isset($trees[$tree_cid])) {

    // Select the links from the table, and recursively build the tree. We
    // LEFT JOIN since there is no match in {menu_router} for an external
    // link.
    $query = db_select('menu_links', 'ml', array(
      'fetch' => PDO::FETCH_ASSOC,
      ->leftJoin('menu_router', 'm', 'm.path = ml.router_path');
      ->fields('m', array(
    for ($i = 1; $i <= MENU_MAX_DEPTH; $i++) {
        ->orderBy('p' . $i, 'ASC');
      ->condition('ml.menu_name', $menu_name);
    if (!empty($parameters['expanded'])) {
        ->condition('ml.plid', $parameters['expanded'], 'IN');
    elseif (!empty($parameters['only_active_trail'])) {
        ->condition('ml.mlid', $parameters['active_trail'], 'IN');
    $min_depth = isset($parameters['min_depth']) ? $parameters['min_depth'] : 1;
    if ($min_depth != 1) {
        ->condition('ml.depth', $min_depth, '>=');
    if (isset($parameters['max_depth'])) {
        ->condition('ml.depth', $parameters['max_depth'], '<=');

    // Add custom query conditions, if any were passed.
    if (isset($parameters['conditions'])) {
      foreach ($parameters['conditions'] as $column => $value) {
          ->condition($column, $value);

    // Build an ordered array of links using the query result object.
    $links = array();
    foreach ($query
      ->execute() as $item) {
      $links[] = $item;
    $active_trail = isset($parameters['active_trail']) ? $parameters['active_trail'] : array();
    $data['tree'] = menu_tree_data($links, $active_trail, $min_depth);
    $data['node_links'] = array();
    menu_tree_collect_node_links($data['tree'], $data['node_links']);

    // Cache the data, if it is not already in the cache.
    cache_set($tree_cid, $data, 'cache_menu');
    $trees[$tree_cid] = $data;
  return $trees[$tree_cid];