0

In the views-view-table.html.twig template, I can get the raw values of fields by repeating on rows and accessing the view object. For example:

    {{ view.field.title.value(view.result[i]) }}   {# the title of the node in this row #}
    {{ view.field.field_link.value(view.result[i]) }}   {# the URL in the link field #}

With Formatter for an image field set to URL to image in the view, I can test to see if an image exists in the field by doing:

    {% if view.field.field_image.value(view.result[i]) %}

QUESTION: How can I get the raw URL for the image? If I use:

    {{ view.field.field_image.value(view.result[i]) }}

I get a number as output. I have tried:

    {{ view.field.field_image.value(view.result[i]).entity.uri.value }}   {# I get nothing #}

and various combinations of things, but haven't hit the right one.

I know I don't have to use the raw values. For example, I can use

    {{ row.columns.field_image.content|render|striptags|trim }}

to get the URL, but this seems like a lot of processing to get something that must be available in the raw result.

11
  • 1
    Add {{ dump(view.result) }} to your template to get a dump of the content. You can then find the URL in there.
    – Jaypan
    Mar 4, 2020 at 1:01
  • Thanks @Jaypan. The dump doesn't contain any URLs. For each row in the dump, the field_image array includes an alt field, a title field, some other fields, and notably a target_id field with integer value that is the same as when I insert {{view.field.field_image.value(view.result[i])}}. Clearly I need to use the target_id to get to the image URL, but I don't know how. I thought {{view.field.field_image.value(view.result[i]).entity.uri.value}} would do it, but it doesn't. Mar 4, 2020 at 5:08
  • 1
    I get it. Your image is an entity reference. You will need to reference that entity. This: {{ dump(view.result[0].entity) }} should show you the values of the referenced entity.
    – Jaypan
    Mar 4, 2020 at 5:17
  • Yes, I thought so too, but the output of {{ dump(view.result[0].entity) }} is NULL. It isn't that view.result[0] is an entity reference; isn't it that view.field.field_image.value(view.result[0]) is an entity reference? Seems so, given that {{ view.field.field_image.value(view.result[0]) }} outputs what appears to be an entity number. Mar 4, 2020 at 6:07
  • 1
    Formatter: url to image, then you can use rewrite results to strip any wrapper html. Mar 11, 2020 at 10:14

2 Answers 2

1

Thanks to all, especially @belltouche and @Jaypan, for taking the time to respond. After all the kind suggestions and lots of reading and trial-and-error, I ended up using the following code in views-view-table.html.twig to display an image URL:

{% for i, row in rows %}
  ...
  {% if view.field.field_image.value(view.result[i]) %}
    {{ row.columns.field_image.content|render|striptags|trim }}
  {% endif %}
  ...
{% endfor %}

Note that I'm using the URL to image formatter for the image field in the view.

To explain, view.field.field_image.value(view.result[i]) gets the raw value of the image field, which turns out to be the entity id for the image. Checking it makes a concise test to see if there is an image in the field (if there's a better way, please say so).

It also turns out that there isn't any other information about the image in the raw value. Just the entity id, which is why I could never find the image URL in the raw value.

There was a suggestion made that I could use Twig Tweak to load the image entity by its id and get the URL from it. I'm sure this would have worked, but since my purpose was to avoid the overhead of render|striptags|trim processing, loading the entity after it had already been loaded to produce the content didn't seem an advantage.

0

For a custom Views Field template, you can access entity field values in the row with a dot structured format with something looks something like this: row._entity.field_name.value for the base entity, or row._relationship_entities.field_name_that_brought_in_the_relationship.field_name_on_the_related_entity.value when accessing something you brought in via Views' relationships. And I've gotten file uris with file_url(...field_imagefieldname.entity.uri.value).

That being said, the comment about using {{ dump(view.result) }} is a good one.

2
  • 2
    Thanks @beltouche. I've tried every combination I can think of based on your suggestion and nothing has worked. I think it's because I'm in a Views Table template instead of a Views Field template, and I'm not using the rows variable, but am instead using the views variable to get raw values. I mention in my question the good results I get for simple fields using the views variable; just can't find the magic to get the raw value of the image URL. Mar 4, 2020 at 6:14
  • 3
    Yes, this doesn't work like a field template. In Views you get either the finally rendered field content or the raw value, which is in your case the target id of the file or media entity. Core Twig doesn't have an option to load the entity, but you can use Twig Tweak to load the file/media entity and access their field values.
    – 4uk4
    Mar 4, 2020 at 7:27

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.