7.x menu.inc menu_tree_set_path($menu_name, $path = NULL)

Sets the path for determining the active trail of the specified menu tree.

This path will also affect the breadcrumbs under some circumstances. Breadcrumbs are built using the preferred link returned by menu_link_get_preferred(). If the preferred link is inside one of the menus specified in calls to menu_tree_set_path(), the preferred link will be overridden by the corresponding path returned by menu_tree_get_path().

Setting this path does not affect the main content; for that use menu_set_active_item() instead.


$menu_name: The name of the affected menu tree.

$path: The path to use when finding the active trail.

Related topics

2 calls to menu_tree_set_path()
menu_test_menu_trail_callback in modules/simpletest/tests/menu_test.module
Callback that test menu_test_menu_tree_set_path().
menu_tree_get_path in includes/menu.inc
Gets the path for determining the active trail of the specified menu tree.


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


function menu_tree_set_path($menu_name, $path = NULL) {
  $paths =& drupal_static(__FUNCTION__);
  if (isset($path)) {
    $paths[$menu_name] = $path;
  return isset($paths[$menu_name]) ? $paths[$menu_name] : NULL;


JohnAlbin’s picture

It should be noted that this was just committed last week and is currently only available in the 7.x-dev release. It will be in Drupal 7.9 and later.

Neue Werte’s picture

Any idea on how to use this with 7.8? Or is there already a release date for 7.9?

Reg’s picture

Take any module you have control over and put this in it:

if (!function_exists('menu_tree_set_path')) {
  function menu_tree_set_path($menu_name, $path = NULL) {
    $paths = &drupal_static(__FUNCTION__);
    if (isset($path)) {
      $paths[$menu_name] = $path;
    return isset($paths[$menu_name]) ? $paths[$menu_name] : NULL;

It's an exact copy of the code with a "if exists" wrapper... I just had to do this today... got a white screen of death from a contributed module I just enabled.

SeriousMatters’s picture

Prior to Drupal 7.9, menu_tree_set_path does nothing since core menu.inc would not call menu_tree_get_path().

Ankabout’s picture

I'm trying to use this function in a view header to set the active trail for my view. Getting the right menu links to be 'active' works perfectly.

I am having a different problem though. I have a block with the code below inside. Basically it gets the parent of the current menu item, and I use the block to display that parent. This doesn't work when using the menu_tree_set_path option. How can I fix this?

  $menuParent = menu_get_active_trail();

  $menuParent = $menuParent[1]['link_title'];
kytom’s picture

Forgive me if this is a simple question, but where should I call it for the change to be picked up by Drupal? Are e.g. tpl.php files too late?
I'm doing this at the beginning of my node--product.tpl.php, which doesn't work:


(in case it's relevant, the menu link is set in Views).

Should I perhaps put it in some preprocessor instead, and if so, which would be the most appropriate?

felixSchl’s picture

I am having the same issue with views paths... I checked in the menu_router table to make sure I am entering the right path for my view, however it won't work.

prinds’s picture

if your view has arguments, you should make sure, that your view page path has % in it.. Example a view using an argument should have 'examplepath/%' in stead of just 'examplepath'..

Perhaps this sorts your problems..

See more here http://www.agentic.ca/blog/menu-active-trails-views-paths-arguments

BarisW’s picture

That was exactly our case too.

ft_evolution’s picture

anyone figure out where to call these?

Madis’s picture

I've used it successfully in my custom module's hook_init() implementation.

lmeurs’s picture

Thanks for the suggestion! First I tried hook_page_process() and hook_page_preprocess(), but neither worked...

fakingfantastic’s picture

You can not use an alias to set the path...

So if you've got "products", but it's real path is "node/123", you must do menu_tree_set_path($menu, "node/123");

If you want to use you're alias, user drupal_get_normal_path($path);

menu_tree_set_path( $menu, drupal_get_normal_path("products") );

Hope that helps...

syn_ack’s picture

I have the same issue. This function does not work with view paths.

By the way: it also works with a path-alias on nodes!

TechNikh’s picture

did you find a solution to make it work with views?

gagarine’s picture

I try this on a menu link pointing on and this make the menu disappear... any idea?

lykyd’s picture

Exactly the same problem. I'm doing :

function menu_path_node_view($node, $view_mode, $langcode) {
if($node->type == 'equipe') {
menu_tree_set_path('main-menu', 'node/398');

In a custom module, but that make my primary links disappear. My menu is called in the block backoffice, not programmatically.

EDIT : I fixed that by using this code in template.php (yourtheme_node_view()) instead of in a custom module.

mlsamuelson’s picture

I was attempting to use this function to add the "active" class to a menu item for another related callback without a menu item (a sub-page, essentially), and I didn't think it was working. But turns out it sets the "active-trail" class on the specified path's item. Not the "active" class.

Worth sharing, as it wasn't immediately apparent to expect this.

Pawlus’s picture

If it doesn't work for you, remember that $path needs to be in internal path format (for example 'node/11', not 'news'). This function can be called in custom module init function or node_view function. It doesn't affect breadcrumbs. It does not always work in template.php.