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.
API page: https://api.drupal.org/api/drupal/core%21modules%21system%21templates%21...
Enter a descriptive title (above) relating to menu.html.twig, then describe the problem you have found:
Attempted multiple times outputting the machine name of the menu, {{menu_name}} but would not get anything.
Using krumo($variables) in the menu preproccesor function did not reveal the variable menu_name, not sure where i can find the variable and pass it to the menu.html.twig
Comment | File | Size | Author |
---|---|---|---|
#9 | interdiff.txt | 1.12 KB | tim.plunkett |
#9 | 2609400-menu-9.patch | 2.33 KB | tim.plunkett |
Comments
Comment #2
benjamin.merkley CreditAttribution: benjamin.merkley at Portage CyberTech commentedComment #3
tim.plunkettNice find!
Comment #4
dawehnerIndeed!
Comment #5
tim.plunkettIn addition to filing this bug, Ben reached out in IRC to help debug this. Please give him a commit credit as well.
Comment #6
effulgentsia CreditAttribution: effulgentsia at Acquia commentedGreat find that menu.html.twig incorrectly documents the presence of a 'menu_name' variable, despite it never being set.
However, I wonder if the better fix is to remove it from the docs, because:
However, I could see use cases for wanting the 'menu_name' variable in the cases where a menu with a machine name is being rendered. I'm just not sure if it makes sense to add that to 8.0 or 8.1. I'm leaning to thinking that we should commit a docs removal into 8.0, then fully add the variable into 8.1. But I'm open to alternate arguments.
Comment #7
effulgentsia CreditAttribution: effulgentsia at Acquia commentedIn any case, I'm ticking the credit box for @benjamin.merkley per #5.
Comment #8
joelpittet@effulgentsia I use that in my projects to help identify menu's on the UL via a class.
Comment #9
tim.plunkettI'm not really sure about 8.0.0 vs 8.0.1 vs 8.1.x, but I think its useful to have it.
Comment #10
effulgentsia CreditAttribution: effulgentsia at Acquia commented@joelpittet: do you mean Drupal 7 or Drupal 8 projects, and how are you accomplishing that, since from what I can tell, the variable isn't set in either version?
Comment #11
joelpittet@effulgentsia in D7 I use menu_block module which provides it at one level. And to get it at the level we are talking about here I use this hook:
THEMENAME_menu_tree($menu_name)
https://api.drupal.org/api/drupal/includes!menu.inc/function/menu_tree/7
Which only provides that argument.
And if I need it on the menu_link I cheat and use the suggestions.
THEMENAME_menu_link__MENU_NAME()
and hardcoded it.https://www.drupal.org/node/254940#theme-suggestions-for-menus
Comment #12
effulgentsia CreditAttribution: effulgentsia at Acquia commentedThanks. #9 addresses #6.2 and #6.3, so re-RTBC'ing it.
Thanks for the info in #11, which speaks to #6.1. I don't know if that's enough to justify doing this during RC, so tagging for triage.
Comment #13
effulgentsia CreditAttribution: effulgentsia at Acquia commentedThat link is to the menu_tree() function, which is not a hook. The signature of THEMENAME_menu_tree() is
https://api.drupal.org/api/drupal/includes%21menu.inc/function/theme_men..., which does not provide the menu_name variable. Or if it does, I don't see where.
Comment #14
joelpittet@effulgentsia whoops, sorry yeah that doesn't come through I should look closer. I use the same hook trickery for that too.
THEME_menu_tree__menu_footer_menu()
https://www.drupal.org/node/254940#theme-suggestions-for-menus
D7
theme_menu_tree()
is pretty bare, not much to actually use for theming there.That
menu name
context data is really nice when you want to pry away some of those wrapper elements for cleaner markup.Comment #15
effulgentsia CreditAttribution: effulgentsia at Acquia commentedDiscussed with @xjm and we decided this is essentially 0 risk and implements something useful that the docs already say is there, so makes sense to do during RC.
Comment #16
alexpottCommitted 9e82672 and pushed to 8.0.x. Thanks!
Comment #20
4aficiona2 CreditAttribution: 4aficiona2 commentedThis seems still to be an issue in Drupal 8.0.2. Tried to output
menu_name
variable in my custom theme, then quick tried it in Bartik without success. It seems that those fixes are not yet part of Drupal 8.0.2. Am I correct or do I miss something?Comment #21
joelpittet@4aficiona2 it's there, just not in the context of the macro which may be a bit confusing.
You can pass it into the macro as another parameter. eg
becomes:
It's like function scope and I'm not sure at the moment how you can access global scope but something you may want to look up, yet still passing it in is the best choice.
Comment #22
joelpittetComment #23
4aficiona2 CreditAttribution: 4aficiona2 commented@joelpittet thanks, I only tried to access
menu_name
inside the macro because I need it within the macro and was not aware of the function scope. But makes totally sense now when I re-read the template comments.It would probably be nice if this gets somehow mentioned in the template comment that
items
andmenu_name
are only available in the global scope and need to get passed into the macro.This makes definitely more sense for someone which gets confronted the first time with a twig macro (in my case the menu_links macro) but I assume that the
menu.html.twig
template could be the first place where somebody gets in touch with a macro and therefore increase the usability of the template.Comment #24
joelpittet@4aficiona2 feel free to open a docs follow-up issue to this to add details around that. It can go into 8.0.x patch release if accepted.
Comment #25
4aficiona2 CreditAttribution: 4aficiona2 commentedopened a documentation issue