menu_set_location

Definition

menu_set_location($location)
includes/menu.inc, line 269

Description

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.

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.

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.

Related topics

Namesort iconDescription
Menu systemDefine the navigation menus, and route page requests to code based on URLs.

Code

<?php
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']);
}
?>
 
 

Drupal is a registered trademark of Dries Buytaert.