Skip to main content
1 of 8
nilsun
  • 577
  • 5
  • 24

How to add class attributes to the anchor element of a given menu by theme function

Do I miss something or is it impossible to theme the link anchor HTML element itself for a specified menu via theme functions? We can theme the <li> elements and the menu block elements with HOOK_menu_link_NAME_menu() and such easely, but I had no luck with targeting the nested anchors itself. All I found was a dead thread and open issue for theme_links(): https://drupal.org/node/588148 - a function I have also tried before to target anchor elements inside a given menu and its links with a debugging test message, but without luck.

https://drupal.org/node/588148 => theme_links() is not really themeable [#588148] => Drupal core, theme system, normal, needs work, 49 comments, 1 IRC mention

theme_links(): Returns HTML for a set of links. => theme_links($variables)

This is the function the issue is about I have posted above. As the issue shows off, it sadly doesn't work very well.

Can anyone point me to another workaround for it? Sorry for asking, but I am a little bit lost here on the half of the way in my scope, because I didn't expected that this could become a problem. I would like to add style class attributes to the element in ul > li of a given menu (not of all menus, thats what theme_links() does)...

And yes, I know the menu_attributes module. It's an awesome module I use very often. But here the limited scope of it all is to solve this directly by a theme function to implement a toggle setting for it later. So I look for a function. I was also looking into the module code of menu_attributes for inspiration but the only function I can imagine being interesting for my approach is hook_menu_link_alter(), but it also seems only to target

  • elements as far as I can see.

    And nested css targeting by altering the upper element is sadly also not an option here, since the class to add is level sensitive and should NOT be applied to the upper html element of <a> in that moment...

  • nilsun
    • 577
    • 5
    • 24