Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
In Views Drupal 6, it was really easy to output the file path of an image.
How do you do this in Views Drupal 7? There doesn't seem to be any options. Ideally, I would like to be able to do it in my tpl.php file.
Comments
Comment #1
big_smile CreditAttribution: big_smile commentedI have worked this out.
1) Create your view as normal.
2) Add your image field as normal.
3) Go to relationships
4) Add Relationship "File Usage"
5) Go back to fields section
6) Under groups, a new option "File" will appear
7) Selecting file will output the path for every file field on your site. If you want it just for one particular image field follows these steps:
7a) Add the new "File" field
7b) In file field, select "exclude from display".
7c) Go back to your image field (created in step 2)
7d) Select "Rewrite the output of this field"
7e) Under tokens, use the token for the file field (created in step 6). (NB the file field must come before the image field, or else the token will not appear).
Hope this helps somebody!
Comment #2
SB CreditAttribution: SB commentedI followed this steps but I don't get a new token in the replacement patterns list
Comment #3
SB CreditAttribution: SB commentedI ended up using image_style_url($style_name, $path)
http://api.drupal.org/api/drupal/modules--image--image.module/function/i...
Comment #4
megan_m CreditAttribution: megan_m commentedRemember that if you want to use a field in a replacement pattern it has to be ordered before the field you're working on (see point 7e above). I was having the same problem and realized that was why.
I played with rewriting the output of the File:path field. However, this is stripping out anything I put in an inline style. For example:
<div style="background: url([uri]) center center no-repeat;"></div>
is striped to just:
<div></div>
I was able to create a view template for the File field and output it as inline CSS that way.
Thanks so much for the tip, big_smile. It helped me out a lot!
Comment #5
g089h515r806 CreditAttribution: g089h515r806 commentedThis module solves that very problem - it's called the Image URL Formatter: http://drupal.org/project/image_url_formatter
Comment #6
stephen Piscura CreditAttribution: stephen Piscura commentedI've spent the whole day trying to solve my problem...without success.
It really sounds like comment #3 by SB is where i need to go, but i'm stuck and would be grateful for any and all advice.
Here's my setup:
public://20110605_007_D50_hannah_love_maternity.jpg
public://20110605_410_D50_hannah_love_maternity.jpg
public://20110605_373_D50_hannah_love_maternity.jpg
public://20110605_067_D50_hannah_love_maternity.jpg
So far so good, but here's what i'm trying to do:
I want to rewrite the output of my "File: Path" field in order to yield its path with a specific image style applied (e.g. sites/media3.dev/files/media/styles/large/public/20110605_007_D50_hannah_love_maternity.jpg). The API link posted by SB seems to explain how one would do that, but the problem is i have NO idea how to integrate this function using the views UI. My PHP skills extend as far as "copy" and "paste," actually. :-) So when i click "rewrite the output of this field," how do i then apply the image_style_url($style_name, $path) function using my chosen image style setting? I was under the impression that one cannot apply PHP within the Views UI without additional modules, but it sounds like i'm mistaken.
Thanks for any and all help!
Comment #7
timofey CreditAttribution: timofey commentedBased on Drupal 7, Views 3
Add a "File Usage: File" relationship (Advanced->Relationsips)
Add a "File: Path" field, NOT "Content: Image" (Block details->Fields)
You will have limitless options on image path output. Click to Display download path instead of URI.
enjoy :P
Comment #8
schmook CreditAttribution: schmook commentedUm...really!? This seems like a lot of steps, for the path to an image. I use this all the time in D6.
Comment #9
dqd@ timofey: As already told you on another issue: Your described way to go with views will break content lists in situations when you use a - let's say - "main" image for the content header part or the teaser view, but also want to use many other images further down in the body text, like examples to the meaning of the content or something similar. Your method will cause duplicate views of the same content-item because of the relationchip file_usage is not modifyable any further and has more than one relation to the content id in this case.
For all how try to figure out image style paths, check the module custom formatters and its included image examples. it shows you how it works and how to use image style paths outside of a image field widget, like in views rewrite output custom html ...
Comment #10
liupascal CreditAttribution: liupascal commentedI'm able to get the path file in my view, but how to do if i have multiple file per entry ?
The goal is to endup with
"SKU" | "Title" | "Images"
sku13| Product 13 | path_to_image1.png, path_to_image2.png....
Anyone managed to do that ? thanks
Comment #11
stephen Piscura CreditAttribution: stephen Piscura commentedTo respond to my own comment, i've created a file view that outputs these fields:
as well as a tpl.php file for my theme (views-view-fields---MY-VIEW-NAME.tpl.php) with this custom code:
so that i get an image displaying in the style a want with true alt and title attributes.
Hope this helps somebody. Sure took me long enough.
Comment #12
garrettfisher CreditAttribution: garrettfisher commentedHi,
How did you do that? I also noticed that my inline style information was being stripped out of the "Rewrite Results" textarea. I now have the file path in the view UI but don't know how to write that into the template. How do I access an individual field in the template?
Thanks,
Garrett
Comment #13
stephen Piscura CreditAttribution: stephen Piscura commented$fields['field_FIELD_NAME']->content
allows you to access the content of your individual field. In the case of your file path, it would look like:$fields['uri']->content
So, if you look at my code above, you'll see that i apply my chosen image style to the file path by using this snippet:
<?php print image_style_url('MY_IMAGE_STYLE', $fields['uri']->content) ?>
Let me know if i'm understanding your question and, more importantly, if i'm helping.
Comment #14
mudds CreditAttribution: mudds commented@Digidog:
Thanks a lot (!!) to your solution with the Custom Formatters.
It solved my issue with displaying Download URLs in content types having multiple files as fields.
Comment #15
jenlamptonI don't think this issue has been resolved. The Field for "File: Path" still does not output a file path in Drupal 7, it outputs a URI.
I agree it may be useful to have a "File: URI" field also, but most people would prefer a path, when they requested a path. For now, my approach to getting a path is to preprocess the results of the cirrent file path field as so:
Comment #16
pipicom CreditAttribution: pipicom commented#7 is the simplest, fastest solution.. Thanks timofey!!!
Comment #17
relish27 CreditAttribution: relish27 commentedI like @timofey's option, too -- it works if I just output the field.
But if I want to output PHP code using the View PHP field and this workaround (http://drupal.org/node/1516348), it still displays the uri.
UPDATE:
Okay, so maybe this was too easy? I just listed my file field ("Report File") in the "Fields" part and then have a "Global PHP" field at the end. In it, I do the following:
file_create_url($data->field_field_report_file[0]['raw']['uri'])
And that returns the web-friendly path!
Comment #18
Danny EnglanderFor #7 above, indeed I get duplicate content as I have more than one image / file. I tried select distinct but that did not work.
Comment #19
funkDoc CreditAttribution: funkDoc commented#7 worked for me, thanks! I can't believe this isn't a option for normal field output.
Comment #20
TheJoker CreditAttribution: TheJoker commentedThis function add dublicate content to views. This bug is active.
Comment #21
skyredwangI believe this featured has been in Views for a while.
#20 We need a mutilple value support for this. But, this would be a seperate feature request.
Comment #22
paultrotter50 CreditAttribution: paultrotter50 commentedIf using the option described in #7 you may wish to tick 'Display download path instead of file storage URI' in in the options for 'file:path' otherwise you may get paths like: Path: public://yourfilename.jpg