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

Members fund testing for the Drupal project. Drupal Association Learn more


benjamin.merkley created an issue. See original summary.

benjamin.merkley’s picture

Issue summary: View changes
tim.plunkett’s picture

Title: Cannot seem to output any of these variables » menu_name missing from menu.html.twig
Component: documentation » menu system
Status: Active » Needs review
1.21 KB

Nice find!

dawehner’s picture

Status: Needs review » Reviewed & tested by the community
Issue tags: +Quickfix


tim.plunkett’s picture

In addition to filing this bug, Ben reached out in IRC to help debug this. Please give him a commit credit as well.

effulgentsia’s picture

Status: Reviewed & tested by the community » Needs review

Great 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:

  • From what I can tell, this variable does not exist in Drupal 7's theme_menu_link() or theme_menu_tree(). #1777332: Replace theme_menu_link() and menu-tree.html.twig with a single Twig template added the docs for it as part of the conversion of the two theme functions into a single template, but I can't find in that issue what the reasoning for that was.
  • The variable is not registered in the 'menu' entry of drupal_common_theme().
  • There are other callers that create a menu-themed render array besides MenuLinkTree, and those callers don't always have a menu_name.

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.

effulgentsia’s picture

In any case, I'm ticking the credit box for @benjamin.merkley per #5.

joelpittet’s picture

@effulgentsia I use that in my projects to help identify menu's on the UL via a class.

tim.plunkett’s picture

2.33 KB
1.12 KB

I'm not really sure about 8.0.0 vs 8.0.1 vs 8.1.x, but I think its useful to have it.

effulgentsia’s picture

@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?

joelpittet’s picture

@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:

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.

effulgentsia’s picture

Title: menu_name missing from menu.html.twig » menu.html.twig says that menu_name is an available variable, but it's not
Status: Needs review » Reviewed & tested by the community
Issue tags: +rc target triage

Thanks. #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.

effulgentsia’s picture

And to get it at the level we are talking about here I use this hook:

That 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.

joelpittet’s picture

@effulgentsia whoops, sorry yeah that doesn't come through I should look closer. I use the same hook trickery for that too.

D7theme_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.

effulgentsia’s picture

Issue tags: -rc target triage +rc target

Discussed 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.

alexpott’s picture

Status: Reviewed & tested by the community » Fixed

Committed 9e82672 and pushed to 8.0.x. Thanks!

  • alexpott committed 9e82672 on 8.0.x
    Issue #2609400 by tim.plunkett, effulgentsia, benjamin.merkley: menu....

  • alexpott committed 9e82672 on 8.1.x
    Issue #2609400 by tim.plunkett, effulgentsia, benjamin.merkley: menu....

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.

4aficiona2’s picture

Version: 8.0.x-dev » 8.0.2

This 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?

joelpittet’s picture

@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

{{ menus.menu_links(items, attributes, 0) }}
{% macro menu_links(items, attributes, menu_level) %}


{{ menus.menu_links(items, attributes, 0, menu_name) }}
{% macro menu_links(items, attributes, menu_level, menu_name) %}

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.

joelpittet’s picture

Version: 8.0.2 » 8.0.x-dev
4aficiona2’s picture

@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 and menu_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.

joelpittet’s picture

@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.

4aficiona2’s picture