1

Hi I am developing a custom module for menus and I want to add active class to menu items based on the current path. For this I have tried theme_menu_link() but this function only provides the mlid(menu link ID) of the original links and not all the menu items. But I need the mlid of all individual menu links.

Is there any alternate hook for this functionality, or any other way to do this?

2

have a look at l() function in includes/common.inc:

...
if (($path == $_GET['q'] || ($path == '<front>' && drupal_is_front_page())) &&
    (empty($options['language']) || $options['language']->language == $language_url->language)) {
  $options['attributes']['class'][] = 'active';
}
...
  • Thanks but I need to retrieve the mlid of all the menu items so that I can match the path based on menu link ids – Somya Gupta Apr 27 '12 at 7:28
  • @SomyaGupta Please consider choose the best answer for your query. This will hep us to close the Question. – niksmac Nov 25 '12 at 14:41
1

Here is my jQuery solution, the advantage is you dont need to add a menu/alter hook.

$(document).ready(function() {
    var pathname = window.location.href;
        $('a').each(function() {
            var cur_a_url = $(this).attr('href');
            if(cur_a_url == pathname) {
                $(this).addClass('active');
            }
        });
});

But THIS is the perfect Drupal way.

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.