| 7 menu.inc | menu_tree_set_path($menu_name, $path = NULL) |
| 8 menu.inc | menu_tree_set_path($menu_name, $path = NULL) |
Set 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()
File
- includes/
menu.inc, line 1166 - 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;
}
Login or register to post comments
Comments
Not available before Drupal 7.9
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
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
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.
It won't work
Prior to Drupal 7.9, menu_tree_set_path does nothing since core menu.inc would not call menu_tree_get_path().
Setting the full trail
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);
?>
Where to call it?
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:
<?phpmenu_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?
views path
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.
thanks in advance
anyone figure out where to call these?
hook_init() is one option
I've used it successfully in my custom module's hook_init() implementation.
Don't use an alias
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...