Skip to main content

Adding an active class to the Drupal primary links

An article from ComputerMinds - Building with Drupal in the UK since 2005
8th Nov 2007

Mike Dixon

Senior Mind
Mike Dixon
Hey, you seem to look at this article a lot! Why not Bookmark this article so you can find it easily in the future?

One of the most annoying things about developing a Drupal theme is the lack of an 'active' class on your primary links. By default the primary links are given a class of menu-X-Y-Z-active if they are active. From a theming point of view, this is next to useless, you need a nice standalone active class on your primary links.

So - here is a very simple snippet for your template.php file ...


function _phptemplate_variables($hook,$vars){
  if ($hook=='page')
    if ($vars['primary_links']){
      foreach ($vars['primary_links'] as $key=>$link){
        if (strpos($key,'-active')){
          $vars['primary_links'][$key]['attributes']['class'].=' active';
        }
      }
    }
  }
}

And thats it - nice and simple, you should now have an active class on your active primary links.

Hi, thanks for reading

ComputerMinds are the UK’s Drupal specialists with offices in Bristol and Coventry. We offer a range of Drupal services including Consultancy, Development, Training and Support. Whatever your Drupal problem, we can help.