-1

I have an entity reference field and the template field--my-custom-entity--field-my-entity-reference.html.twig

In the template I print the content.

{% for item in items %}
  {{ item.content }}
{% endfor %}

How do I print fields created on this entity reference field?

Despite wishful thinking, this does not work: {{ item.content.field_my_field }}, nor does any of the 100 other possibilities I tried. Twig dump() was not helpful.

  • Don't do it! Use proper entity and field templates, use the same method for your referenced entity as you do on your normal host entity. Entities are cached render arrays, you would be bypassing a lot of security and performance logic built into Drupal core. – Hudri Aug 13 at 7:38
  • What exactly is your goal? Are you just trying to print a couple fields from the referenced entity? – sonfd Aug 13 at 11:20
  • 1
    Not quite sure about the downvotes. Maybe it's not clear what I'm asking but it I don't see how it's in any way unreasonable to want to access fields on an entity reference field. This is a "proper" field template. It's a field template for an entity reference. Not sure how your concerns apply to my question @Hudri. – John Aug 13 at 15:27
  • 1
    To add just a class you are better off preprocessing that into the attributes. As a rule of thumb only do templating when you really need to change the markup (even this can often be accomplished with the Field Group module). – leymannx Aug 13 at 21:34
  • 1
    I appreciate what you're saying here. There's no way we could use the admin ui and view modes alone to build this site, which is from a robust parts kit of probably 100+ different components, with multiple classes on every html element. Not to mention classes that wire up Javascript for tabbed units, sliders, accordions, etc. The templating system and custom entities is the the only feasible/sane way to do it. If I were simply spitting out some text from a wysiwyg, then maybe view modes and {{ content }} in a template could do the trick. That's rarely the lack of complexity I'm afforded. – John Aug 14 at 1:25
2

To get the entity reference's field values:

  1. Change the display of your field to Rendered Entity like the following enter image description here
  2. Now you have access to your entity values in your twig inside item.content for example if your entity reference on content you will find #node key inside item.content array for example you can render field_my_field like the following.

     {% for item in items %}
      {{ item.content['#node'].field_my_field.value }}
    {% endfor %}

Update:
In case of custom entity change #node by #my_entity_type

| improve this answer | |
  • This works thanks. In my case it was item.content['#my_entity_type'].field_my_field.value – John Aug 13 at 15:35
  • Any thoughts on comments above saying to NOT do this? – John Aug 13 at 15:36
  • It's still not clear what your goal is. You say you want to add a class with the entity id, but which entity id (parent or referenced) and where do you want to add the class? You want to avoid changing something inside the referenced node here because caching strategies don't expect it. If you try to change something inside the referenced node on one page, and then the same referenced node is rendered on a second page, the cached version from the first page will be used on the second page. This is the caching issue @Hudri is mentioning above. – sonfd Aug 13 at 16:34
  • My goal is to add a class with the entity-id around every item in a multi-value entity reference field. The entity reference can reference any number of bundles. So rather than put this wrapper class in 10+ bundle templates, I want to put it in one place. My use case goes deeper, but that is a sufficient explanation. As I see it, I can do this 1 of 3 places. 1. The 10+ bundle templates (not ideal) 2. The parent entity template (not ideal, I have to loop through and render the entity references one by one. 3. The entity reference field template, which you are advising against. – John Aug 13 at 20:50
  • Further, from what you're saying, I can surmise that even having that entity ref field template in my codebase is a bad idea? Because regardless of whether or not I access fields in it, it's still printing {{ content }}. – John Aug 13 at 20:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.