1

I am writing a template for a views field in Drupal 8. How can I access the raw values of a multi-value field in views? I tried this but it doesn't work.

{% for i in 0.. row.field_color['#items']|length - 1 %}
{{ row.field_color[i] }}
{% endfor %}
  • Try something like {{ row.field_color[i]|raw }}, check example here. – Yogesh Dec 23 '16 at 5:20
  • Can you please specify which twig you are working on?? – Ashish Deynap Dec 23 '16 at 5:20
  • 1
    This is probably the equivalent of a raw value: {{ row.field_color[i]|striptags }} – No Sssweat Dec 23 '16 at 9:31
  • I working in views-view-fields--styles.html.twig. Styles is the name of the view. – david Dec 25 '16 at 23:16
2

Since you are using it in views-view-fields--styles.html.twig, it would be {{ fields.field_color.0 }} or {{ fields.field_color.content|striptags }}.

| improve this answer | |
1

Use the entity to get the raw values from the database:

{% for item in row._entity.field_color %}
  <p>{{ item.value }}</p>
{% endfor %}
| improve this answer | |
  • That did not display anything. – david Dec 25 '16 at 23:16
  • Tested it in views-view-fields--myview.html.twig. It works. If you have a view configured on the entity the field is in, for example node. Test this with {{ row._entity.entitytype.id }} – 4k4 Dec 26 '16 at 0:20

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.