Menu Item Theming - can we alter <li>?

duncano74 - January 22, 2008 - 05:33

Hi - I want to use "sliding doors" from alistapart.com to create menu tabs across the top of a page - almost got it to work - for the final part I need to find a way to add the "active" class to the LIST ITEM element as well as the LINK ELEMENT

e.g. to go from this.....

<ul>
      <li>
             <a class="active">MENU ITEM</a>
      </li>
</ul>

to this.............
<ul>
      <li class="active">
             <a class="active">MENU ITEM</a>
      </li>
</ul>

Looks like it should be simple enough, but I'm not that familiar with Drupal or PHP to charge straight in....

THanks in advance :)

Pop something like this into your template.php file

mdixoncm - January 22, 2008 - 07:17

function phptemplate_menu_item($mid, $children = '', $leaf = TRUE) {
  if (menu_get_active_nontask_item() == $mid) {
    $class_extra = 'active';
  }
  return '<li class="'. ($leaf ? 'leaf ' . $class_extra : ($children ? 'expanded ' . $class_active : 'collapsed')) .'">'. menu_item_link($mid) . $children ."</li>\n";
}

You could also check to see if the current menu item was in the active trail - this is handy if your dealing with tabs, i.e. you want an active state on a menu item when one of it's children are active ...

Mike,
Computerminds offer Drupal development, consulting and training

is there a variable name to change?

duncano74 - January 23, 2008 - 04:25

I've dropped that in there, but no immediate result - is there a variable name that requires I should change in there somewhere? Bear in mind I know only rudimentary PHP and nothing about how Drupal templating works (yet...)

btw - Thanks Mike - much appreciated

This works for me

letsGoOn2 - January 27, 2008 - 22:35

Thanks to the O.P. for asking this, and thank you Mike for your timely response. Your PHP does exactly what I needed!

As to why it isn't working for the original poster, I only wish I could help... All I can say is that the <li> for my currently selected menu has class "leaf active". The child <a> within the <li> is still classed as "active", but that's just fine for what I'm doing.

Thanks again!

 
 

Drupal is a registered trademark of Dries Buytaert.