2

I have a content type with a multiple referenced field and I need to iterate trough each item and display the item information in a table but I don't know and didn't find a way to access the content field information like the value of a specific field.

Example: I have a content type called Task and in Task I have multiple Entity Reference to Worker content type, Worker has title, name and phone. How can I iterate each worker and display the information in a table?

What I want to do is to override node--task.html.twig to do something like this:

Assuming that we have a field_workers field in task content type.

<table>
   <thead>
     <tr>
       <th>
         Name
       </th>
       <th>
         Phone
       </th>
     </tr>
   </thead>
   <tbody>
    {% for worker in content.field_workers %}
     <tr>
       <td>
        {{worker.field_name.value}}
       </td>
       <td>
         {{worker.field_phone.value}}
       </td>
     </tr>
     {% endfor %}
   </tbody>
</table>

Is it possible? Thank you!

  • Just nipping out so can't check, but is worker.entity defined in the loop? If so it's probably just a matter of using {{worker.entity.field_name.value}} etc – Clive Mar 10 '17 at 19:10
  • That did not work. Probably the worker entity is not defined in the loop, can't tell. – Henrique Alho Mar 13 '17 at 10:20
3

You want to work with node instead of content.

something like this should work

{% for item in node.field_workers %}
 {{ item.entity.field_name.value }}
{% endfor %}

See Entity API Cheat Sheet and my blogpost for more information: http://www.md-systems.ch/en/blog/techblog/2017/02/20/twig-and-entity-field-api-or-how-to-take-control-of-nodehtmltwig

  • Thank you Berdir, this solved my problem! Sorry about my late reply, I was not at work. Trying to avoid another question or more search, do you know how may I be able to format dates and access the {{ author_name }} of that item? Thank you. I also would like to have the quick edit|edit|delete|manage display button in each row of my table, is it possible? – Henrique Alho Mar 13 '17 at 10:24
3

As @Berdir explains in http://www.md-systems.ch/en/blog/techblog/2017/02/20/twig-and-entity-field-api-or-how-to-take-control-of-nodehtmltwig you have two options, use the node entity (see Berdir's answer) or the render array in content. For the content approach you have to put the table row in a second node template:

node--task.html.twig

<table>
   <thead>
     <tr>
       <th>
         Name
       </th>
       <th>
         Phone
       </th>
     </tr>
   </thead>
   <tbody>
     {{ content.field_workers }}
   </tbody>
</table>

node--worker--table.html.twig

     <tr>
       <td>
         {{ content.field_name }}
       </td>
       <td>
         {{ content.field_phone }}
       </td>
     </tr>

Disadvantage obviously is you need two templates. Advantage is, you can render the Worker fields how they are configured in UI and you can use text filters. Use a separate view mode table, so that you can display the Worker content type in other places as normal. You might also need to remove html from the field--field-workers.html.twig template if it's in the way of the table structure.

  • Hi, thank you for your answer and sorry about my late reply. I liked your solution but I could only manage to solve it with Berdir answer. Best regards! – Henrique Alho Mar 13 '17 at 10:22
  • Building tables in templates can get quite complicated, it might be easier to build a table in Views and attach it to the node with drupal.org/project/eva – 4k4 Mar 13 '17 at 10:32
  • there was a syntax error in the first template, corrected it – 4k4 Mar 13 '17 at 10:58
  • Yes I noticed that. Thank you for tour suggestion, I think that what I need to do is solved with templates but I will take a look if I need to add complexity! – Henrique Alho Mar 13 '17 at 14:18

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.