Search code examples
drupaldrupal-6menu

How do I dynamically set the active menu item?


For example, I have "node/31". And I want drupal to set menu active item to "galleries/lastgallery" if node/31 is viewed. How to do it?

<?php
function custom_nodeapi (&$node, $op, $a3 = NULL, $a4  = NULL){
    if ($op == 'view' && $node->nid == 31) {
           ???
        }
}
?>

Updated: I know I can use menu_set_active_item. But in this case tabs (View/Edit etc. tabs) will be not displayed. I believe it should be done with menu_set_active_trails, or may be menu_get_item/menu_set_item somehow, but I can't figure how exactly.


Solution

  • For similar purpose, I used menu_set_item(). You should leave the $path argument NULL and use the menu item for the internal path of 'galleries/lastgallery' as $router_item.

    function custom_nodeapi(&$node, $op, $a3 = NULL, $a4  = NULL) {
      if ($op == 'view' && $node->nid == 31) {
        $path = drupal_get_normal_path('galleries/lastgallery');
        if($path && $menu_item = menu_get_item($path)) {
          menu_set_item(NULL, $menu_item);
        }
      }
    }