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

big_smile’s picture

Status: Active » Closed (fixed)

I 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!

SB’s picture

I followed this steps but I don't get a new token in the replacement patterns list

SB’s picture

I ended up using image_style_url($style_name, $path)
http://api.drupal.org/api/drupal/modules--image--image.module/function/i...

megan_m’s picture

Remember 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!

g089h515r806’s picture

This module solves that very problem - it's called the Image URL Formatter: http://drupal.org/project/image_url_formatter

stephen Piscura’s picture

I'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:

  1. Views is setup with relationship of "File Usage: File" (A file that is associated with this node, usually because it is in a field on the node.)
  2. I've added the field "File: Path" (using the above-mentioned "File Usage: File" relationship)
  3. And my output looks something like this:

    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!

timofey’s picture

Based 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

schmook’s picture

Um...really!? This seems like a lot of steps, for the path to an image. I use this all the time in D6.

dqd’s picture

@ 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 ...

liupascal’s picture

I'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

stephen Piscura’s picture

Version: 7.x-3.x-dev » 7.x-3.0
Component: Miscellaneous » files/upload data

To respond to my own comment, i've created a file view that outputs these fields:

  1. file URI
  2. title text field i've attached to my file via File entity (fieldable files)
  3. alt text field i've attached to my file (same method as above)

as well as a tpl.php file for my theme (views-view-fields---MY-VIEW-NAME.tpl.php) with this custom code:

<img
	src="<?php print image_style_url('MY_IMAGE_STYLE', $fields['uri']->content) ?>"
	alt="<?php if (!empty($fields['field_MY_ALT_FIELD'])) print $fields['field_MY_ALT_FIELD']->content ?>"
	title="<?php if (!empty($fields['field_MY_TITLE_FIELD'])) print $fields[field_'MY_TITLE_FIELD']->content ?>"
>

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.

garrettfisher’s picture

Hi,

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

stephen Piscura’s picture

How do I access an individual field in the template?

$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.

mudds’s picture

@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.

jenlampton’s picture

Title: Output file path of image » File "Path" field needs to output a file path, not a URI (and perhaps add a File: URI field too)
Version: 7.x-3.0 » 7.x-3.x-dev
Category: support » bug
Status: Closed (fixed) » Active

I 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:

    if ($vars['field']->field_alias == 'file_managed_uri') {
      $vars['output'] = file_create_url($vars['row']->file_managed_uri);
    }
pipicom’s picture

#7 is the simplest, fastest solution.. Thanks timofey!!!

relish27’s picture

I 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!

Danny Englander’s picture

For #7 above, indeed I get duplicate content as I have more than one image / file. I tried select distinct but that did not work.

funkDoc’s picture

#7 worked for me, thanks! I can't believe this isn't a option for normal field output.

TheJoker’s picture

This function add dublicate content to views. This bug is active.

skyredwang’s picture

Status: Active » Closed (fixed)

I 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.

paultrotter50’s picture

Issue summary: View changes

If 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