views:

45

answers:

3

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.

A: 

I'm not sure if you want to change the active item to affect which page is loaded (which would need to be done early in the page load to make a difference), or if you simply want to superficially highlight the "galleries/lastgallery" link as if it were the active link.

If you just want to make it appear as if the "galleries/lastgallery" link is the active one, that can be done by overriding the template_preprocess_page() function. There, you can rebuild the primary (or secondary) menu and add logic to give the link to "galleries/lastgallery" the "active" (and/or "active-trail") class.

James
A: 

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);
    }
  }
}
mongolito404
A: 

I was able to achieve something simular to what you are trying to do by using a combination of menu_set_active_item() and menu_execute_active_handler()

It took a little while to realize that I needed both. I was using a custom menu handler to create "clean urls" from Solr filter combinations. the $path variable is the internal drupal path that I am hiding, and the menu handler is what the user sees in their browser.

I then used custom_url_rewrite_outbound() to convert all relevant search links on the site to the format I wanted.

Below are the relevent parts of the code. I'm not sure if this is what you are looking for, but it might give you some ideas. I was impressed at how much you could control what the user saw and what Drupal was aware of internally. The proper menu items were active, and everything else worked as expected.

function myModule_search_menu(){
  $items['browse/%menu_tail'] = array(
    'title' => 'browse',
    'description' => 'Search Replacement with clean urls.',
    'page callback' => 'myModule_search_browse',
    'access arguments' => array('search content'),
    );
}

function myModule_search_browse(){
   /* do lots of fancy stuff */
   menu_set_active_item($path);
   return menu_execute_active_handler($path);
}
mirzu

related questions