function menu_tree_set_path

You are here

7 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.

Parameters

$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.

File

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

Code

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;
}

Comments

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.

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

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.

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

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?

<?php
  $menuParent
= menu_get_active_trail();

 

$menuParent = $menuParent[1]['link_title'];
 
print_r($menuParent);
?>

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:

<?php
menu_tree_set_path
('main-menu','products');
?>

(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?

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.

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

That was exactly our case too.

anyone figure out where to call these?

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

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

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...

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!

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

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

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.