It's another person requesting help theming links. Yay! I'm sure this has been asked before but Google and searching the Drupal site did not turn up the answer.

Recently I worked on a project where we had to do a lot of restyling on the primary links for a conference website. After investigating in Firebug I saw that the menu tree structure goes

link.expanded
leaf
leaf
leaf

link.collapsed

leaf (free standing link, no children)

What I would like is a way to custom class theme for the leafs under link.expanded, the leafs under link.collapsed, and have a separate class for leafs that have no children. Right now, the fact that the leaf class is reused all over the place is causing a lot of problems.

I know this can be accomplished with PHP but alas I don't know much in that area. I'd like to avoid using a menu module if possible and perhaps use a code snippet if someone has already thought of this. I'm sure this has been asked many times but I after several searches I did not find what I was looking for. I have found this reference http://api.drupal.org/api/function/theme_links but I do not know how to modify it to assign classes in the way I need.

Any help would be greatly appreciated.

KWebb

Comments

dcc1’s picture

I remember reading about this in the forums. I tried looking for it cant find it. Search again - I know its in here.

Good luck.

wfx’s picture

I did search again and I'm still not finding the answer..

corneliusk’s picture

this will give you a start. I've used depth for this before, but the code below is untested.


function  YOURTHEMENAME_menu_item_link($link){
  if (empty($link['localized_options'])) {
    $link['localized_options'] = array();
  }
  $link['localized_options'] += array('attributes'=> array());
  if(!isset($link['localized_options']['attributes']['class'])){
    $link['localized_options']['attributes']['class'] = '';
  }
  if($link['depth'] === 1){
    $link['localized_options']['attributes']['class'] .= 'level_'.$link['depth']; 
  }
  return l($link['title'], $link['href'], $link['localized_options']);
}


?>

wfx’s picture

Thanks! This gives me something to go from, much appreciated.

wfx’s picture

I was looking for a way to code this functionality into my theme but I found this module and it seems to work pretty well.
http://drupal.org/project/menu_attributes

The Menu Attributes module allows you to specify a class and ID per menu item within the menu system in Drupal. Very useful!! I've searched a thousand times trying to find a module adding this functionality and I stumbled on it totally by accident when I wasn't even looking anymore.