Tell me more ×
Drupal Answers is a question and answer site for Drupal developers and administrators. It's 100% free, no registration required.

I want to set a menu item of the main menu as active (class="active").

I'm specifically looking for a hook or function, not a contrib module.

I have seen recommendations for Context, and I have looked in the module code, but it uses hooks which I cannot find on the API-Site.

share|improve this question

3 Answers

If you simply want to add the 'active' class to a menu item you could do it in the code before printing the menu. If you want the menu item to also act like an active item you could use menu_tree_set_path() that was supposedly added in Drupal 7.9.

In earlier versions and Drupal 6 I used this:

function mymodule_init() {
  $item = menu_get_item($_GET['q']);
  $item['href'] = 'node/1';
  menu_set_item(NULL, $item);
}

However both options just add the 'active-trail' (not 'active') class(es), but that should suffice in most cases.

share|improve this answer
This seems not to work. In the navigation menu the tree is expanded, but the entry in the main-menu is not active. – rekire Jan 21 '12 at 16:10
If you mean 'active' class then yes that one doesn't get added with this code, but it gets 'active-trail' that should be helpful. If you mean something else then I'd need a bit more information of what you are trying to achieve. – Dooshta Jan 21 '12 at 16:54
I have two taxonomies which splits the page in two parts. With seo stuff I managed that the two taxonomies have their own path e.g. example.com/tag1 and example.com/tag2 which is linked in the main-menu in the header. A example for content which is in the taxonomy tag1 is aviable at example.com/tag1/welcome. Now I want that the link in the main menu in the head is still active. – rekire Jan 21 '12 at 17:19

You could use menu_set_active_trail to set which menu should be rendered as active.

Another way is to mess with $_GET['q'], but you really want to avoid that if at all possible, since it can lead to some strange bugs.

share|improve this answer
Could you add a small example for menu_set_active_trail? – rekire Jan 21 '12 at 11:00
@rekire look at book_build_active_trail for inspiration (D6); – googletorp Jan 21 '12 at 11:13
up vote 1 down vote accepted

My solution is this:

function mymodule_init() {
    $link=menu_link_load(346);
    $link["options"]["attributes"]["class"]="active";
    menu_link_save($link);
}

The id 346 is the mlid no idea what this mean but I got this id with this little dump:

echo('<pre>');
dump(menu_tree_all_data('main-menu'));
echo('</pre>');
share|improve this answer
Surely your solution will give the item with mlid (Menu Link Identifier for your reference) 346 the "active" class regardless of what is ACTUALLY the active link? Is this your intended behaviour? – Chapabu Feb 5 '12 at 17:15

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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