Same name and namespace in other branches
  1. 4.7.x includes/menu.inc \menu_set_location()
  2. 5.x includes/menu.inc \menu_set_location()

Change the current menu location of the user.

Frequently, modules may want to make a page or node act as if it were in the menu tree somewhere, even though it was not registered in a hook_menu() implementation. If the administrator has rearranged the menu, the newly set location should respect this in the breadcrumb trail and expanded/collapsed status of menu items in the tree. This function allows this behavior.

Parameters

$location: An array specifying a complete or partial breadcrumb trail for the new location, in the same format as the return value of hook_menu(). The last element of this array should be the new location itself.

This function will set the new breadcrumb trail to the passed-in value, but if any elements of this trail are visible in the site tree, the trail will be "spliced in" to the existing site navigation at that point.

Related topics

5 calls to menu_set_location()
blog_view in modules/blog.module
Implementation of hook_view().
comment_reply in modules/comment.module
forum_view in modules/forum.module
Implementation of hook_view().
hook_view in developer/hooks/node.php
Display a node.
theme_forum_display in modules/forum.module
Format the forum body.

File

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

Code

function menu_set_location($location) {
  global $_menu;
  $temp_id = min(array_keys($_menu['items'])) - 1;
  $prev_id = 0;
  foreach (array_reverse($location) as $item) {
    if (isset($_menu['path index'][$item['path']])) {
      $mid = $_menu['path index'][$item['path']];
      if (isset($_menu['visible'][$mid])) {

        // Splice in the breadcrumb at this location.
        if ($prev_id) {
          $_menu['items'][$prev_id]['pid'] = $mid;
        }
        $prev_id = 0;
        break;
      }
      else {

        // A hidden item; show it, but only temporarily.
        $_menu['items'][$mid]['type'] |= MENU_VISIBLE_IN_BREADCRUMB;
        if ($prev_id) {
          $_menu['items'][$prev_id]['pid'] = $mid;
        }
        $prev_id = $mid;
      }
    }
    else {
      $item['type'] |= MENU_VISIBLE_IN_BREADCRUMB;
      if ($prev_id) {
        $_menu['items'][$prev_id]['pid'] = $temp_id;
      }
      $_menu['items'][$temp_id] = $item;
      $_menu['path index'][$item['path']] = $temp_id;
      $prev_id = $temp_id;
      $temp_id--;
    }
  }
  if ($prev_id) {

    // Didn't find a home, so attach this to the main navigation menu.
    $_menu['items'][$prev_id]['pid'] = 1;
  }
  $final_item = array_pop($location);
  menu_set_active_item($final_item['path']);
}