I have a view that is not related with a menu but I need to add .active class to an element of this menu when the view is shown. How can I alter the element adding a class active?


You can use the Context module for this. Context is capable of adding active classes to menu items based on conditions, e.g. path, content type, etc.

  • Thank you, the truth is I'm not familiarized with context module, can you please be a litter more specific on how can I achieve that? – yuki Mar 25 '14 at 22:17
  • Actually it was very simple, it solved my situation. Thank you! – yuki Mar 25 '14 at 22:48
  • Good to hear you already figured it out. You're welcome! – Sidney Gijzen Mar 26 '14 at 15:22
  • @yuki Can you also explain us what you did to resolve the situation in your case? Thanks! – stdcerr Apr 26 '14 at 7:44

The function you want is menu_tree_set_path(), and you also need menu_get_item() to figure out if to use it, eg:

function mymodule_base_init() {
  $item = menu_get_item();

  if ($item['page_callback'] == 'views_page') {
    if ($item['page_arguments'][0] == 'myview' && $item['page_arguments'][1] =='mydisplay) {
      menu_tree_set_path('menu-mymenu', 'some/arbitrary/path/in/the/menu');
  • Is the function menu_tree_set_path() available in versions prior to Drupal 7.9? – yuki Mar 25 '14 at 22:08
  • @yuki According to the API docs, it isn't. However, as of 2013-03-25, the current version is 7.26. There have been a lot of security related issues and bugfixes since Drupal 7.9 was release on 2011-10-26. – mpdonadio Mar 25 '14 at 22:16

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.