Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
Hello. I am trying to figure out how add classes to menu items via a custom module. The menu_attributes.api.php file seems to only cover adding new attributes that can be set for menu items.
Thanks!
Sean
Comments
Comment #1
reign85 CreditAttribution: reign85 commentedUp. I need it too.
It will be nice if we can use it programaticaly liek this:
But maybe another way to do this exist? without using the UI
Comment #2
dobe CreditAttribution: dobe commentedYou can, I just tried and tested this. Since the attributes are stored in the menu_links.options column of the database. You can do this with your example above.
Let me know if this doesn't work for you. As it has worked for me.
Comment #3
ADrupalUser CreditAttribution: ADrupalUser commentedIs it possible to set the attributes dynamically? I'd like to colorize a menu option (by adding a css class) only under certain conditions.
Comment #4
dobe CreditAttribution: dobe commentedProbably best way to do this is via javascript. The context module may be able to do this as well for you.
-Jesse
Comment #5
ADrupalUser CreditAttribution: ADrupalUser commentedThanks. In my case, I was able to use Menu CSS Names instead, as the menu text also changes when it needs to be colorized.
Comment #6
geerlingguy CreditAttribution: geerlingguy as a volunteer commentedAlso, if you just want to programmatically update an existing link (but not modify it in hook_menu or _alter...):
Comment #7
dqdFollow up #2524398: Support for UI based custom attributes
(related issue added)
Comment #8
dqdAnother suggestion: use https://www.drupal.org/project/menu_fields. This maybe renders this feature/support request obsolete?
Comment #9
joelpittetAnswered in #6 and more solutions in #7 and #8