2

With THEME_menu_tree() we can change the markup of the <ul> element and with THEME_menu_link() we can add css classes to the items of it easely. But what if we have an IF IS IN THIS BLOCK condition to incept?

Not THEME_menu_tree nor THEME_menu_link() know in which block the menues are, they alter, because both will alter all menus known by system.

Two ways came in mind, but both were dead ends: 1) pass a variable from THEME_preprocess_block() to THEME_menu_tree() and THEME_menu_link() -> No, makes no sense. Still, they don't know what to do with it nor can they follow the foreach() of THEME_preprocess_block(), because they have no conditions for blocks. 2) Finding a higher entry level to pass the condition over and alter the menu attributes and its item attributes from there.

Well. Good point. But where would that be? Printing out the $variables inside THEME_preprocess_block() was like counting drops in the ocean. Can someone please enlighten me if I am right with the second thought and with where to look at?

2 Answers 2

4

There are actually functions available for specific menus too:

Targeted overrides (suggestions) available for theme_menu_link() and theme_menu_tree()

In addition to other changes to menu rendering, a Drupal 7 theme can implement a THEMENAME_menu_tree__MENU_NAME() and/or THEMENAME_menu_link__MENU_NAME() function to override theme_menu_tree()/theme_menu_link() for a specific menu. For example, THEMENAME_menu_link__management() would override theme_menu_link() for links within the "Management" menu. This is similar to how "node--article.tpl.php" overrides "node.tpl.php".

Source: https://drupal.org/node/254940#theme-suggestions-for-menus

5
  • 2
    This is awesome! Thanks David. I didn't know that. It isn't exactly what I was asking for, but it could become definitely a work around to change the theme settings UI from the originally wanted "unknown menus in block regions" concept to a more "fixed known menues to alter" kind of. My actually approach was to leave it unfixed how many menus will be there and that's why I wanted to make regions and blocks playing the conditions part for the setting. But if this isn't possible I definitely will try it that way. Thank you very much in this late hour :) (here in Berlin)
    – nilsun
    Jun 28, 2013 at 0:09
  • You're welcome :) it was a reading between the lines solution that should work well there. Jun 28, 2013 at 0:58
  • 1
    David, sadly this is no option for me after a longer inspection. I was wondering why it doesn't take affect and then I humbled over the issue: It seems not to work with main-menu and others which have double names. Look at the issue in front of your link text at d.o. On the end of the discussion after the close-tag comes a late user report, which fits exactly to the experience I shortly made with this.
    – nilsun
    Jun 28, 2013 at 1:24
  • Some themes use theme_links instead, in which case you could try theme_links__system_main_menu Jun 28, 2013 at 1:35
  • 2
    David, I forgot to mention that I got it working with THEMENAME_menu_link__MENU_NAME() to add classes to the li elements! Thank you for pointing me in the right direction. ... Thank you very much. - Now I struggle with getting a level deeper: I need to get hands on the anchor element inside of the li element and that seems to be a much harder fight. But, thank you very much for all of your input!
    – nilsun
    Jul 3, 2013 at 9:50
1

For clarification: The answer (actually provided by David Thomas) to this question is, that it doesn't need to get fired in preprocess_block(), since THEMENAME_menu_link() offers the option to append the menu name with a double __ to target a special menu. Finally that makes more sense of course, since menus can (will) move into other blocks, but that was far away from my hope it could work. Not every code can be perfect. But it does.

Using THEMENAME_menu_link_MENU_NAME() did the trick.

Thanks to David Thomas who has pointed that out to me. I am not sure if the d.o. docs are mentioning this. I am still a little bit lost in the Drupal docs, but it gets better. A big system has big docs of course. I just have to learn how the doc system is build, to find my issues.

Thank you, David.

1
  • Please note that MENU_NAME in THEMENAME_menu_link__MENU_NAME() should be separated with 2 underscores from the rest of the function name
    – daniels
    Feb 8, 2019 at 13:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.