Drupal: change a menu item’s class in module
So I just spent 3 hours writing 4 lines of code.
The problem: we need a way to make a certain “Approvals” link red if there are approvals waiting. This could also apply to making an “Inbox” link stand out if you have unread messages or whatever. Anyway, according to Google, this is something that has never been done before in the history of computing, so here it goes.
The hook we’re using is hook_translated_menu_link_alter() a.k.a. the_longest_hook_name_that_drupal_has_ever_dreamed_of(). Basically, that hook grabs menu items before they’re displayed so you can change them however you want. Changing them is tough though, particularly because it’s not documented at all.
So here’s the code.
So as you can see here, I’m counting all the reservation nodes which are unapproved, and if there are any, I grab that specific menu item and give it a class of ‘attention’. The tough part here is the ‘localized_options’ part. The API would have you believe that that part should just be ‘options’ but it’s all a big lie.
Maybe it has something to do with the locale module? Anyway, there you go.