2

I have a section (call news) that have an "home page". This home page is created via "views" and via "views" I have insert a link item into "main menu" menu based on path (/news). When I go to /news the relative entry in "main menu" is active (the <li> item has "active-trail" class). All works.

How can I active the same menu when I'm into a node of type news ? I have read that I need to use menu_tree_set_path() into a hook_preprocess_page(). So I have tried to add this line into THEME_preprocess_page(&$vars) :

menu_tree_set_path('main-menu','/news');

but it does not work. I have tried with:

menu_tree_set_path('main-menu','news');

Same result. Not working.

In all my search, all tutorials tells to set for the second parameter of menu_tree_set_path a node/NID or a taxonomy/term/NID, but I have not this node/nid. I have a view page path.

So, How can I do it ? Thanks.

1

I guess you could achieve this with Menu Position module, where you can set an active parent for specific node/content types.

https://www.drupal.org/project/menu_position

1

I had to do a trick like that once. I just used menu_set_active_item() but I had to manage the breadcrumb display too and here is my piece of code for that:

// Activate the View menu item but memorize the actual nature of the node for the breadcrumb
function mymodule_node_view($node, $view_mode, $langcode) {
  if ($node->type == "news" && $view_mode == "full") {
    $_SESSION['mymodule_active_trail'] = serialize(menu_get_active_trail());
    $_GET['mymodule_node_id'] = $node->nid;
    menu_set_active_item("news");
  }
  else {
    if (isset($_SESSION['mymodule_active_trail'])) {
      unset($_SESSION['mymodule_active_trail']);
    }
  }
}
// Alter the breadcrumb to show the real path to your news node
function mymodule_menu_breadcrumb_alter(&$active_trail, $item) {
  if ($item && $item['path'] == "news") {
    if (isset($_GET['mymodule_node_id'])) {
      if (isset($_SESSION['mymodule_active_trail'])) {
        unset($_SESSION['mymodule_active_trail']);
      }
    }
    if (isset($_SESSION['mymodule_active_trail'])) {
      $original_active_trail = unserialize($_SESSION['mymodule_active_trail']);
      if (is_array($original_active_trail)) {
        $total = count($active_trail);
        $active_trail[$total] = $item;
      }
    }
  }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.