0

It seems I'm able to print text field values with {{ view.field.field_example.getvalue(view.result[0]) }}, but this same method doesn't work for image fields. Please help.

3
  • What value from the image field do you want to print ? Have you looked at drupal.stackexchange.com/questions/224294/… or drupal.stackexchange.com/questions/219039/… ?
    – GiorgosK
    Commented May 24, 2019 at 19:37
  • I just want to render the actual image itself. The solutions you linked don't seem to work. I'm not sure if it's because those solutions are for the template page.html.twig whereas I'm using views-view.html.twig. Sorry -- I am pretty new to Twig and Drupal 8.
    – Bri
    Commented May 24, 2019 at 20:39
  • Sorry perhaps I did not read your question carefully.
    – GiorgosK
    Commented May 25, 2019 at 5:26

1 Answer 1

0

views-view.html.twig is not meant to be used for field printing.

You should use views-view-fields--[view-name].html.twig

OR

views-view-fields--[view-name]--[machine-name].html.twig if you want to be even more specific.

and there you just do

{{ fields.field_name.content }}


For multi value fields, is there a way to print just one value within a field at a time?

{% set field_values = row._entity.get('field_name').getValue() %}
{% for field_value in field_values %}
    <li>{{ field_value['value'] }}</li>
{% endfor %}

By doing {{ fields.field_color_hex.content }}, I printed them all at once.

In your case:

{% set hexes = row._entity.get('field_color_hex').getValue() %}
{% for hex in hexes %}
    <li>{{ hex['value'] }}</li>
{% endfor %}
2
  • I was using views-view-fields.html.twig before, but I found I couldn't print individual values within a field. For instance, I have a field called field_color_hex which contains multiple hex values for different colors. By doing {{ fields.field_color_hex.content }}, I printed them all at once. Is there a way to print just one value within a field at a time?
    – Bri
    Commented May 24, 2019 at 21:22
  • @Bri updated answer.
    – No Sssweat
    Commented May 24, 2019 at 22:02

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.