Get the src of the image field in a view in drupal
Solution 1
You can get the file name by changing the field as shown in the attached image.
Formatter: Rendered file
View mode: Tokens (or Link will work - depending on what modules you have installed)
Strip HTML tags (checked)
You'd then need to do a Rewrite Output with the server's public file location in front of the token [field_first_image]. That'll get you the full SRC.
There's probably a better way to do this though... Depending on the final thing you're attempting to create.
Solution 2
If you don't want to need to install a module for this, there is a solution.
- Create a new relationship
- Select your image field (this will create a relationship to the file entity)
- Add a field to your view
- Select
File: Path
- Check
Display download path instead of file storage URI
Note that this will create a URL to the original image, not to an image produced when applying an image style (like cropping, resizing etc.) so this may not work in every scenario.
Solution 3
This module add a url formatter for image field. Then you can output image url directly.
Solution 4
One possibility is to create a custom field formatter for the image field and use this formatter in the set-up of the view.
In your case, the custom image field formatter shall only return the source of the src of the image.
You can use this great tutorial to create a custom field formatter : http://www.computerminds.co.uk/articles/creating-new-field-formatters-drupal-7
Hari K T
Owner Tanvish Technologies Freelancer (PHP, Golang, WebRTC) Speaker Writer Open-source lover and contributor Scribbles at harikt.com, sitepoint.com, acodersdream.com, contributes to Aura Project for PHP, love tweeting or going through some interesting questions in stackoverflow.
Updated on December 19, 2020Comments
-
Hari K T over 3 years
We were working with Drupal 7 and trying to rewrite the views output
We need the src of the image field , not the whole image tag.
Clicking on
Rewrite the output of this field
[field_first_image]
: Gives the whole content ie with tag ,We tried to display the below ones
[field_first_image-fid]
== Raw fid[field_first_image-alt]
== Raw alt[field_first_image-title]
== Raw titleBut the same ( [field_first_image-fid] ) thing is displayed in the view. Is there a way we can get the src from the views rewrite ?