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.

Command icon 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

rhache created an issue. See original summary.

ozin’s picture

Did you check this article https://lembergsolutions.com/blog/get-fieldable-drupal-menu-menu-item-ex..., maybe you will find an answer there.

rhache’s picture

Unfortunately that page does not offer enough details.

voleger’s picture

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

voleger’s picture

StatusFileSize
new670 bytes
chrishk2015’s picture

I found I was able to access the fields like the following:

{% for item in items %}
   {{ item.content['#menu_link_content'].field_my_field.value }}
{% endfor %}
divined’s picture

It is value only, but how to get rendered array? How to display formatted output?

divined’s picture

Need to change:
MenuLinkTreeHandler:91

$render_output = $view_builder->view($entity, $view_mode);

to

$render_output = array_merge(
  $view_builder->view($entity, $view_mode),
  EntityViewDisplay::collectRenderDisplay($entity, $view_mode)->build($entity)
);

Don't forget to use EntityViewDisplay;

and item.content.any_field will be available in the twig.

letrollpoilu’s picture

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

marcaddeo’s picture

StatusFileSize
new979 bytes

Here's a patch for the changes suggested in #8

ozin’s picture

Status: Active » Needs review
adsyy’s picture

#10 work for me thanks !

joris_lucius’s picture

#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!

  • voleger committed 950ac7e on 8.x-2.x
    Move patch [#3034814-10] into the issue fork

  • voleger committed c6d7e8c on 8.x-2.x
    Issue #3034814 by voleger, marcaddeo, rhache, ozin, divined: Output...
voleger’s picture

Status: Needs review » Fixed

Thanks. Fixed

Status: Fixed » Closed (fixed)

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