11

The image formatter has only one option: Image. I would like to get the URL, not the full markup, for a field output.

Do I need to make a custom formatter? Also, I don't see any way to rewrite the text using tokens to get the image URL.

3
  • There is a module image url formatter that only has a drupal 7 version. Your only other option at the moment is to port that module over to D8. Nov 26, 2014 at 20:09
  • It makes me feel dirty, but I'm just going to regex it out of the markup on the client side for now. Nov 27, 2014 at 22:35
  • I will love to do this without the "Image URL Formatter." Jan 10, 2016 at 6:02

5 Answers 5

14

When setting up your view, under the advanced section you can create a relationship for the image field.

After you added the relationship, you can then add a File URI field, select the created relationship and you are good to go.

1
  • 2
    I much prefer this method than installing yet another module for something that should be out-of-the-box functionality. Jan 17, 2017 at 14:51
8

This is the magic, without the "Image URL Formatter" module:

  1. You have to add a relationship in your view to your image field (It's under Advanced)
  2. Now you can add a 'File URI' field to your fields
  3. In your views-view-fields.html.twig template (Or one of the appropriate alternatives for your specific view) add this:

{{ file_url(fields.uri.content|striptags|trim) }}

2

Is the module "Image URL Formatter" maybe helpful for you? https://www.drupal.org/node/1162614/release?api_version%5B%5D=7234

There is a second one: https://www.drupal.org/project/image_raw_formatter

I never did a feature comparison to find out which is the better one ...

1
  1. Install the module twig_tweak
  2. Create a relationship in a View
  3. Add File URI field, exclude it from display
  4. Overwrite result, with the next twig code:

    {{ uri | image_style('thumbnail') }}

Where uri is the token of the File URI field and thumbnail is the desired image style.

1

Here is my solution that doesn't require a relationship:

  • Add your image field to your view
  • Set these properties:
    • Formatter: Image
    • Image style: None (original image)
    • Link image to: File

This gives you an output like this: <a href="http://domain/path/to/file.jpg" ...

  • Under Rewrite results, check "Override the output of this field with custom text"
  • In the text box put this: {% if field_image|trim is not empty%} {{ field_image|split('"')[1] }} {% endif %}

This splits the string on the " character and the index of 1 is your url in the href. It is kinda hacky but this actually gave me what I wanted. Going the relationship route I could only ever get a link with public:/path/to/file.jpg not the absolute URL

1
  • I just tried the relationship way and it works. I also got the full url to the image which is good enough for me. I did this: {{ file_url(fields.uri.content|striptags|trim) }} in my fields template.
    – Robbiegod
    Aug 15, 2019 at 20:17

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.