Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get image url from uri in Drupal 8 template

I'm trying to create my own image field formatter. I've followed these instructions (http://www.sitepoint.com/creating-custom-field-formatters-drupal-8/) and right now I have a working formatter which basically does nothing but "inherits" and executes default image formatter.

Then I of course have my own template which is also a copy of default image-formatter.html.twig template (found that in core\modules\image\templates).

Now my question is that in template I can print the whole image using Twig syntax {{ image }}. If I use dump(image) I see there's actually image uri like this: public://default_images/Default image for Drupal Ant task.jpg

I'd love to have the actual url instead but right now I have no idea how to get it. Is it even possible? And I would also appreciate if you could explain me shortly why {{ image }} prints image with img tags? It must go through another formatter?

Why I started to do this in a first place was that instead of img tags I'd like to use divs with background image style.

like image 902
kivikall Avatar asked Nov 11 '15 20:11

kivikall


People also ask

How do I find the URL of an image in Drupal 8?

Use the Drupal admin to configure where the background image will appear: https://www.drupal.org/project/bg_image_formatter. Use a preprocess to generate the URL and the media query: https://www.drupal.org/docs/8/modules/responsive-background-image/how-to-use-the-responsive-background-image-module.


1 Answers

There is a twig extension file_url you can use for this, for example

{{ file_url(content.field_image['#items'].entity.uri.value) }}
like image 124
varta Avatar answered Oct 15 '22 02:10

varta