API page: https://api.drupal.org/api/drupal/core%21modules%21system%21templates%21...
Problem/Motivation
Right now it is not obvious that menu_name
is only globally available and it needs to get passed into the menu_links
template menu.html.twig
to render e.g. in the toplevel ul
.
This is probably only necessary for themers / developers who hit the first time a twig macro (was it for me) but menu.html.twig
might be one of the first templates with which a themer / developer gets in contact and would ease the usability of the template.
I described my issue here and @joelpittet cleared it up.
Proposed resolution
Mention in the template header / comments that menu_name
needs to get past into the menu_links
macro because it is only globally available like items
.
Comment | File | Size | Author |
---|---|---|---|
#33 | mention_in_the_comments-2649076-33.patch | 2 KB | dimaro |
#33 | interdiff-2649076-27-33.txt | 1.94 KB | dimaro |
#27 | menu-context-comment-2649076-27.patch | 2 KB | JohnAlbin |
Comments
Comment #2
4aficiona2 CreditAttribution: 4aficiona2 commentedComment #3
4aficiona2 CreditAttribution: 4aficiona2 commentedComment #4
joelpittetThanks for creating this issue. One thing to note is likely you wouldn't be using that menu_name class on every ul/li. So more practically speaking you should use it outside the macro surrounding the call to the macro.
eg.
Or better:
Comment #5
4aficiona2 CreditAttribution: 4aficiona2 commentedThis would be one option, but if you want to use it within the toplevel
ul
which has already a special case in the template (also no need for an extra wrapper element) it would still be necessary to pass it into the macro like below.Either way, it might be good to mention that
menu_name
is only globally available for twig newbies (like me).Comment #6
joelpittetDo you have a proposal for how the comment should read? I'd like to have something short and to the point that can explain that since it's really the only macro used in core (except for the variations for book and toolbar which do the same thing).
Comment #7
4aficiona2 CreditAttribution: 4aficiona2 commentedSure, I completely understand. My suggestion would be:
Old comment:
* - menu_name: The machine name of the menu.
New comment:
* - menu_name: The machine name of the menu (if needed within macro like items, pass it too into the macro as an argument).
In my opinion this gives an extra hint to be aware that menu_name needs to get passed in the macro even if it does not mention explicitly that menu_name and items are only globally available. This could be an option too to mention that those two are only globally available but I tried to keep it as short as possible. I know, its over 80 chars.
I hope this is a viable solution. What is your opinion @joelpittet? Too much of an edgecase?
Comment #8
star-szrYeah this is a tricky one, we don't want to confuse things either. I would lean towards more of a message saying that menu_name isn't passed into the macro by default. There are three places that you'd need to change to make menu_name available in all levels of the macro so not sure how we explain that.
Or what if we just pass menu_name into the macro so it is available? Would that break anything? Macro arguments are optional so seems like it would be fine…
Comment #9
4aficiona2 CreditAttribution: 4aficiona2 commentedSure, if
menu_name
also gets passed into the macro then the additional comment is obsolet.items
andmenu_name
would be available outside and within the macro which was what I initially assumed (might others too which face a twig macro for the first time too).And in my opinion it should not break the existing templates. If it gets outputted too, e.g. on the toplevel
ul
, is another question and should be up to the themer. But in my case I like to outputmenu_name
as a class name to distinguish the different menus without an extra wrapper element.Comment #10
star-szr@4aficiona2 if you're up for it I think uploading a patch changing all the menu and menu-like templates to include menu_name as relevant would be a good next step. We can at least see if any of the core automated tests fail as a result of that, and go from there.
Comment #11
laughnanDon't know if we are leaning toward just updating the comment (see attached). As a pseudo novice to the theme layer (and not finding documentation specifically on it), where are the twig macros to modify?
Comment #12
laughnanAnd whoops. My comment patch was supposed to be geared more toward the 'not by default' response. Updating.
Comment #13
joelpittet@laughnan re #11 the macro is the function looking thing in that file.
http://twig.sensiolabs.org/doc/tags/macro.html
We've opted to keep it in the menu.html.twig and self import it as to avoid finding some generic place to put it and still have it available to the theme system template overrides.
The documentation patch will be easy to change in > 8.0.x. If we actually change the macro that will be 8.1.x material and may not be worth your while as the use-case for it may be slim(I could be wrong here). I'm completely on board with clear documentation to help people out though.
Comment #14
star-szrAdding it to the macro might be more of a curiosity at this point, but also interesting to think about best practices for macros and how much variables should we be passing in. Or should we always pass in the _context like the docs show, that's another option.
My new idea for the docs side is to use @code to show how the macro would need to be changed. As I said three lines need to be changed and I don't think trying to explain that in words will be effective.
Comment #15
laughnanThanks for that @joelpittet. Forever learning, forever learning...
Comment #16
joelpittetAren't we all;)
Comment #17
radubutco CreditAttribution: radubutco as a volunteer commentedComment #18
radubutco CreditAttribution: radubutco as a volunteer commentedI've added a patch which passes the menu_name into the macro, as mentioned in #8.
Please review.
Comment #19
iamartin CreditAttribution: iamartin as a volunteer commentedTest should be performed using Stark theme because fix has already been applied to Classy, which rolls down to Bartik.
Test passed.
Comment #20
joelpittet@radubutco I don't think we should change this. My thought is that it's not common need to add the menu name to all ul/li/a and that could cause some CSS cascading issues in styles due to the recursiveness of this macro. So I'd much rather like the title says, add notes to why it's not available.
Comment #21
tstoecklerComment #23
thamasTo inform those who are arrive here because of facing the problem of using
menu_name
I wrote a blog post related to the problem (and using the information) of this issue: https://medium.com/integral-vision/drupal-8-twig-add-custom-css-classes-...Comment #24
example_mentee CreditAttribution: example_mentee commentedBased on joelpittet's comment in #20, The scope of this issue should be to only add a comment in the twig template that explains that {menu_name} can be added to the Twig call. The patch should not actually attempt to change the default Twig code.
Novice tasks:
Comment #25
laughnan@example_mentee is then comment/patch #12 sufficient?
Comment #26
JohnAlbinI don't think the patch in #12 is sufficient to explain the actual problem.
This patch adds the comment where the problem exists, inside the macro itself.
Comment #27
JohnAlbinHmm… http://twig.sensiolabs.org/doc/tags/macro.html uses the term "arguments" instead of "parameters". We should keep the terminology the same. Here's a corrected patch.
Comment #28
thamas@JohnAlbin Maybe it would be nice to help newbies by mentioning that we are thinking of the variables listed in the intro comment of the file. Or is it obvious?
Comment #30
dimaro CreditAttribution: dimaro at La Drupalera by Emergya commentedAdd test against 8.2.x on #27
Comment #31
vanessakovalsky CreditAttribution: vanessakovalsky as a volunteer commentedThis patch seems good to me and apply without problems
Comment #32
star-szrs/aditional/additional/
Perhaps we should add to this documentation that already exists in these files?
I'm actually not sure what our policies are on this, but I wonder if we should consider creating docs on drupal.org and linking to them from these files? It would be easier to provide sample code and explain recursion, scope, etc. in that format, rather than a small inline comment.
Comment #33
dimaro CreditAttribution: dimaro at La Drupalera by Emergya commentedPatch to change aditional for additional.
@Cottser as you said on #32 the documentation already exists in these files.
We should change the latest patch to remove the extra documentation and finally do something like this?:
Comment #34
star-szr@dimaro thanks! I'm suggesting we consider expanding that existing documentation. So not removing documentation but adding/combining with the existing docs in these files.
Comment #35
dimaro CreditAttribution: dimaro at La Drupalera by Emergya commented@Cottser thanks for the explanation.
It may be interesting to include all the information.
However, I think that we should update the scope of this issue to do that, right?
Comment #45
Amber Himes MatzThis issue came up and was discussed in a #documentation group triage session.
We think this issue is really about the concept of what a macro is and an initial confusion about how they work. There is readily discoverable information both in Twig's docs and the Drupal Wiki (https://www.drupal.org/docs/theming-drupal/twig-in-drupal/macros-in-twig...) about what a Twig macro is and how to use it.
If anything, examples from menu's template files could be added to the Drupal Wiki page on macros.
Adding documentation to every usage of macro (there are 12 at last count) in Drupal core template files creates an undue maintenance burden.
Closing as "won't fix" with an encouragement to review and edit (if necessary) the Drupal Wiki page on the subject: https://www.drupal.org/docs/theming-drupal/twig-in-drupal/macros-in-twig...
Thank you all for your input and work on various patches.