menu_set_active_item

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

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

Parameters

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

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.

Related topics

3 calls to menu_set_active_item()

File

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

Code

function menu_set_active_item($path) {
  $_GET['q'] = $path;
}

Comments

clarification

this phrase: "which determines which page is loaded" scared me to think that this causes a redirect or something.. I'm assuming this may be the case when called at hook_boot() or somehing, but I've used this function to trick menus into showing a different active state for system pages.

this is useful if your system paths are part of some visual site architecture you're going for that doesn't really exist because of the preset system architecture..

for example.. /node/%/edit is pretty locked down.. but if you want your edit page to look like it's part of a different menu system, you can use hook_nodeapi(), $op=='prepare' and then call this function to set your menus to make it look to the user that you're within some other part of the site.

EDIT: This will kill your local tasks... (view/edit links) lame

EDIT:
to preserve local tasks ( at least when calling this from nodeapi ), just call menu_local_tasks(0) before calling menu_set_item(), instead of menu_set_active_item()...

see this: http://stackoverflow.com/questions/3087141/drupal-how-to-set-active-menu...

Reposting Solution

I'm going to re-post the stack overflow solution here. It worked great for me! I was able to use it in my contemplate too.

<?php

//Sets the active menu item to foo/bar
$path = drupal_get_normal_path('foo/bar');
$menu_item = menu_get_item($path);
if(
$path && $menu_item) {
 
menu_set_item(NULL, $menu_item);
}
?>

not work in Drupal 7.12

not work in Drupal 7.12

Possible to use in theme_preprocess_page

I figured out this solution, so I could have active menu item based on node taxonomy term. Is there any problem using it in template.php?

<?php
theme_preprocess_page
(&$variables) {
   if(isset (
$variables['node']->type) && $variables['node']->type == 'article')  
     
$tid = $node->field_article_taxonomy['und']['0']['tid'];
     
$term = taxonomy_term_load($tid);
     
menu_set_active_item('taxonomy/term/' . $term->tid);
   }
}
?>

Login or register to post comments