4

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.

4 Answers 4

2

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.

3
  • 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, 2012 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.
    – Madis
    Jan 21, 2012 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, 2012 at 17:19
1

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.

2
1

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>');
1
  • 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, 2012 at 17:15
1

You can also use hook_translated_menu_link_alter()

/**
 * Implements hook_translated_menu_link_alter()
 */
function Module_translated_menu_link_alter(&$item, $map) {

    if ($item['menu_name'] == 'menu-name') {

        //check first two arg from url
        $path = arg(0)."/".arg(1);

        //add class active-trail if path match
        if (strpos($item['link_path'], $path) !== false) {
            $item['in_active_trail'] = true;

        }

    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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