I'm trying figure out if there's a way of only printing certain fields in the menu-levels.html.twig file, like you can in a node template override.
I was assuming that something like {{ item.content.field_name }} would work, and I'm having a hard time figuring where the fields are and how to output them when looking at kint array output.
I was also hoping to figure out how to only print children, but not any fields. I have a template for a specific level (menu-levels--main--level-0.html.twig) where I only want the children to output, but not any additional fields, even if filled out. Not outputting children is easy ({{ rendered_content|without('children') }}), but only printing children has proven difficult.
| Comment | File | Size | Author |
|---|---|---|---|
| #10 | output_individual_fields-3034814-10.patch | 979 bytes | marcaddeo |
| #5 | 3034814-05-test-only.patch | 670 bytes | voleger |
Issue fork menu_item_extras-3034814
Show commands
Start within a Git clone of the project using the version control instructions.
Or, if you do not have SSH keys set up on git.drupalcode.org:
Comments
Comment #2
ozinDid you check this article https://lembergsolutions.com/blog/get-fieldable-drupal-menu-menu-item-ex..., maybe you will find an answer there.
Comment #3
rhache commentedUnfortunately that page does not offer enough details.
Comment #4
volegerFor that reason, we had implemented the viewmode field. So the scenario is that you define the viewmodes (possible menu item views that can be used in the menu) and set a reference to the desired view mode for the required menu item.
Comment #5
volegerComment #6
chrishk2015 commentedI found I was able to access the fields like the following:
Comment #7
divined commentedIt is value only, but how to get rendered array? How to display formatted output?
Comment #8
divined commentedNeed to change:
MenuLinkTreeHandler:91
$render_output = $view_builder->view($entity, $view_mode);to
Don't forget to use EntityViewDisplay;
and
item.content.any_fieldwill be available in the twig.Comment #9
letrollpoilu commentedI couldn't make #8 work...
Could you please make a patch out of it?
I fully agree that this feature is important, I don't see the point of rendering all the fields at once, I want to have some in my li classes, others in the content which is quite difficult now...
Comment #10
marcaddeo commentedHere's a patch for the changes suggested in #8
Comment #11
ozinComment #12
adsyy commented#10 work for me thanks !
Comment #14
joris_lucius#10 also worked for me: after applying the patch, {{ item.content.field_icon }} was working for me in override "menu-levels--main--level-1.html.twig"
thanks!
Comment #17
volegerThanks. Fixed