1

I have a field collection field having four fields.

I want to display the record in tabular format.

What I have done.

Created a field twig template file.

Added the code like

{% for item in items %}
    <div{{ item.attributes }}>{{ item.content }}</div>
{% endfor %}

Twig file is working properly. But i need to display the content in tabular form.

Coming like

enter image description here

Required

enter image description here

Available Variables

enter image description here

5
  • Can you please provide the available variables in the twig template for a better understanding? Commented Oct 13, 2016 at 10:23
  • Thanks for reply . How to check the available variables?
    – DRUPWAY
    Commented Oct 13, 2016 at 10:26
  • did u check this drupal.stackexchange.com/questions/192064/… Commented Oct 13, 2016 at 10:43
  • Putting only <div></div> will not give you a table structure. Please refer to dynamic tables for some basic help.
    – AkiShankar
    Commented Oct 13, 2016 at 13:29
  • Thanks @AkiShankar actually i am stop in how to run for loop in field collection twig files and print each field value.
    – DRUPWAY
    Commented Oct 13, 2016 at 14:12

1 Answer 1

1

There is not only one twig template, there are three levels of twig templates in a field collection field:

1) The field template for the field-collection field: field--field-collection.html.twig

2) The field-collection template itself: field-collection-item.html.twig

3) The field templates for the fields inside of the field collection: field--field-collection-item--field-field1.html.twig

When you are in the top level, the fields of the field collection are not built yet and you have no access to the rendered fields.

The best approach is probably to put the table structure in all three levels, in 1) the header and footer, in 2) the row and in 3) the cells.

There is one problem with this approach. In 1) you don't have the labels for the header. If you don't want to hardcode them, you can try to get them from the configuration of the field collection in a preprocess hook.

1
  • Thank you i will try this approach it may take sometime rnd because I am new to twig.
    – DRUPWAY
    Commented Oct 13, 2016 at 13:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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