Score:0

How do I use {{file_url()}} to get image field URLs in views-view-fields.html.twig?

tr flag

I tried to use the following code in views-view-fields.

{{ file_url(fields.field_featured_image.content.0['#item'].entity.uri.value) }}

When I use that code, I get this error.

TypeError: Argument 1 passed to Drupal\Core\File\FileUrlGenerator::generateString() must be of the type string, null given, called in /home/vito/Desktop/D9-fabco-v2/fabco-v2/vendor/twig/twig/src/Environment.php(418): eval()'d code on line 45 in Drupal\Core\File\FileUrlGenerator->generateString() (line 58 of core/lib/Drupal/Core/File/FileUrlGenerator.php).

What's the correct way to use {{ file_url() }}?

Score:0
tr flag

I have solved that in other way:

In created views and in there have the image filed enter image description here

Than changed the image formatter enter image description here

Than in views-view-fields.html.twig

<div class="image" style="background-image:url({{fields.field_featured_image.content|striptags|raw}});"></div>

be-careful with quotation marks and spaces that can cause why your image not displayed.

If you have better solution please do not hesitate post it here in comments

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.