Over the Christmas break I decided that I’d had enough of hard coding theme functions to add attributes to specific menu items in Drupal, so I finally took the time to write a module that would do the heavy lifting. The outcome is the Menu Attributes module.
All the module does is give you the ability to add attributes to the menu item <a> elements, but this has been something I've wanted in Drupal for a long time now.
The main reason I wrote the module was to add the rel="nofollow" attribute to certain menu items (learn why you should be using nofollow to direct PageRank to high value pages), but it made sense to allow users to set other valid attributes as well. So far I have included the Id, Name, Target, Rel, Class, Style, and Accesskey attributes which can be added to any menu item. If you think other attributes would be useful, let me know I’m happy to consider including them.
In the next couple of weeks or so I’ll be releasing an update to include an admin page so that you can choose which attributes are available on the menu form so as not to overcrowd the form with items that you’ll never use.
It’s worth noting that the actual attributes are stored in the menu_links table in the options field of the menu item itself so no new databases are needed and no additional processing done when building the menu. The one drawback of this is that there is no way to mass reset the attributes you’ve set using the module, because it is stored alongside other attributes that are set by other modules.
If you’ve got any other uses or feedback for the module, I’d love to hear them in the comments.