Author image
Senior Mind

Adding an active class to the Drupal primary links

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.