7 menu.inc menu_set_active_item($path)
4.6 menu.inc menu_set_active_item($path = NULL)
4.7 menu.inc menu_set_active_item($path = NULL)
5 menu.inc menu_set_active_item($path = NULL)
6 menu.inc menu_set_active_item($path)

Sets the active path, which determines which page is loaded.

Note that this may not have the desired effect unless invoked very early in the page load, such as during hook_boot(), or unless you call menu_execute_active_handler() to generate your page output.

Parameters

$path: A Drupal path - not a path alias.

Related topics

2 calls to menu_set_active_item()
drupal_deliver_html_page in includes/common.inc
Packages and sends the result of a page callback to the browser as HTML.
user_page in modules/user/user.pages.inc
Page callback: Displays the user page.

File

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

Code

function menu_set_active_item($path) {
  $_GET ['q'] = $path;
  // Since the active item has changed, the active menu trail may also be out
  // of date.
  drupal_static_reset('menu_set_active_trail');
}

Comments

The parameters section suggests invoking the menu_set_active_item() function during hook_boot(), but this leads to an 'undefined function' error in my module. Is there another hook that can invoke menu_set_active_item() or drupal_access_denied() early on in the page load?

Specifically, I'm trying to layer on an additional, external security mechanism (fed from a non-Drupal cookie), and I want to be able to deny access to pages as early on as possible.

I'm using this function within my theme at template.php

function MYTHEME_preprocess_page(&$vars) {
if(!empty($vars['node'])){
switch ($vars['node']->type) {
case 'article':
//change MY_TRAIL for your Drupal path (can be aliased, don't need full path)
menu_set_active_item('MY_TRAIL'');
break;
case 'page':
menu_set_active_item('pages/index');
break;
}
}

Thanks carvalhar! This saved me hours! Using this to set a menu item generated by hook_menu to active for a certain node type - thereby faking it as the parent...

Thanks for this.

If you have something like :
"articles/myArticle" and you use the code above in order to have for example :
"/articles" active on your menu, this will change your arg(0) and (arg1) on corresponding nodes !

In my example, arg(0) will be "articles" and arg(1) will not exist.

If you use "menu_set_active_item" for your menus, you seems to kill the function which using arg(), like menu_get_object() which we generally use to get the $node in a block...
So be aware :(

Me again. So like I said before, menu_set_active_item() is changing your "q=node/123" by "articles". So you don't have arg(0) = "node" and arg(1) = 123, but instead arg(0) = articles because of menu_set_active_item("articles").

So I really advise you to use instead menu_tree_set_path() like this :

if(!empty($variables['node'])){
        switch ($variables['node']->type) {
            case 'article':
                menu_tree_set_path('main-menu','articles');
                break;
            case 'other':
                ...
        }
}

It will active the link "/articles" only in the menu "main-menu" and it will not change q=node/123, so you can use like usually arg() and menu_get_object(), or current_path(), etc ! :)