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?
2 Answers
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.
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?– yukiMar 25, 2014 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, 2014 at 22:16