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 %}
4
  • Try something like {{ row.field_color[i]|raw }}, check example here.
    – Yogesh
    Commented Dec 23, 2016 at 5:20
  • Can you please specify which twig you are working on?? Commented Dec 23, 2016 at 5:20
  • 1
    This is probably the equivalent of a raw value: {{ row.field_color[i]|striptags }}
    – No Sssweat
    Commented Dec 23, 2016 at 9:31
  • I working in views-view-fields--styles.html.twig. Styles is the name of the view.
    – david
    Commented Dec 25, 2016 at 23:16

2 Answers 2

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

1

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

{% for item in row._entity.field_color %}
  <p>{{ item.value }}</p>
{% endfor %}
2
  • That did not display anything.
    – david
    Commented Dec 25, 2016 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 }}
    – 4uk4
    Commented Dec 26, 2016 at 0:20

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.