I doubt this is the best way to implement this, but it's the quickest and I want to upload a patch to use.

While the mechanism of providing this support may change (for example, switching to to hook_theme_suggestions_HOOK_alter), the result (menu--sitemap.html.twig as a template suggestion) should not change.

Comments

akalata created an issue. See original summary.

akalata’s picture

StatusFileSize
new3.15 KB

Unfortunately, until #2511548: Add a "context" array variable to all theme hooks and "#context" array property to all elements to provide optional contextual data lands (which would allow us to include a #context element when declaring #theme => menu), I think we're stuck with the initial approach.

This patch re-adds the menu-specific menu__[menu_name] suggestion, and adds a sitemap__menu__[menu_name] suggestion.

digitaldonkey’s picture

Patch works as expected. Allows overriding the template again.

pauleb’s picture

Thanks for the patch!
I can confirm that the result of the patch from comment #2 is as expected.

But I'm not sure if the template suggestions when enabling twig debug are correct.
After applying the patch and clearing the caches the new template is used, but the file name suggestion looks like:

<!-- FILE NAME SUGGESTIONS:
   x menu--sitemap.html.twig
   * menu--sitemap--main.html.twig
   x menu--sitemap.html.twig
   * menu--main.html.twig
   * menu.html.twig -->

I'm not deep into theming, but as I understand it the "x" marks the used template and menu--sitemap.html.twig should not appear two times with different specificity in the suggestions.

It still works for me, but I am not sure if this is a problem for someone else.
I'm using Drupal 8.9.6 and a bootstrap_barrio - bootstrap_sass - subtheme.

Edit: While this behaviour appears on the main menu, it does not on a custom menu I created.

  • akalata committed f0def7a on 8.x-2.x
    Issue #3153982 by akalata: Support a sitemap-specific menu template
    
akalata’s picture

Status: Needs review » Fixed

@pauleb I'm pretty sure that is an active core issue: #2881195: Duplicate template suggestions.

Status: Fixed » Closed (fixed)

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