After searching for a while to try to determine how to create a custom template file (tpl.php) for a FileField field, I FINALLY stumbled upon this thread: http://drupal.org/node/508388 . What I'd like to try to do with this post is to use the concepts outlined in that post to show you how to accomplish this more specific task.
What I wanted to accomplish was to create a view where one of the fields was a WMA file using FileField. However, instead of showing the default icon and filename in the WMA column, I wanted to display a single, custom icon for each file that would link to the file it was representing.
1. Build the view
If you're unfamiliar with this part of the process, I'd recommend looking around for a tutorial. Basically, I just created a node view with a Style of "Table", and stuck in the fields that I wanted to display. Here's the key, though, and the thing that most people miss:
Add the FID relationship
When you add your FileField field (in my case, it was a field called WMA that I used to upload WMA audio files), you must also add a Relationship for Content: (your field).
Add the "File: Path" field
Next, you should add File: Path to the Fields list, using the relationship that you just created. You can do this by clicking "+" plus sign in the Fields section of the Edit view form, and select "File: Path" as the field you would like to add. Next, under "Relationship", choose to use the relationship that you just created. For me, this was called "WMA Audio". Finally, check the "Exclude from display" checkbox. (Remember to save your view!)
What this does is that it makes the filepath information available within the them file in PHP. Otherwise, all you would have had to work with would have been the file's FID, which wouldn't have gotten you anywhere without hitting up the database with some PHP.
2. Create the theme file.
I wanted to be very specific about which field I was theming, so I ended up copying the contents of "modules/views/theme/views-view-field.tpl.php" to "sites/all/themes/mytheme/views-view-field--broadcast-list--field-wma-fid.tpl.php". What this did for me was to ensure that file that I was theming was within my view, which I had named "broadcast_list". If you're not sure what your template file should be named, just got to the Edit View form and click on Theme: Information. Hopefully you can find what you need from there.
Theme your field
You should now have access to the main variables that you would like to theme. My suggestion would be to add this line to your views-view-field.php template file:
<?php print_r($row); ?>
That way, you should be able to see all of the right variables to print out as you build your html. For me, the final product looked something like this:
<?php if($row->files_node_data_field_wma_filepath): ?>
<a href="<?php echo $row->files_node_data_field_wma_filepath; ?>" title="Download WMA Sermon Audio"><img src="/<?=$directory?>/images/media_icons/icon_wma.gif" /></a>
<?php endif; ?>
So, for each WMA FileField, it printed out a neat little icon that linked to the file it was representing.
Hope that helped! Just thought I'd get that out there, since it would have saved me a lot of time. Let me know if you have any questions!
Comments
How about more than 1 file?
Thanks for this info - however in my case it's not quite working, as I have multiple files and try as I might, I can't get this relationship to output more than a single file, unless I set the delta to 'all', in which case I then get duplicate rows.
Can you help?
Hello. Just wanted to point
Hello.
Just wanted to point out that there is a great screencast tutorial introducing views row theming-- ideal for those new to this subject... oh the possibilities.
Thank you
I wanted something much simpler, just to replace the name of the file with the word Download. I'd looked at rewriting the output of the field, but I hadn't thought to add a relationship in order to be able to access the file path. This very quickly allowed me to figure out what I needed. Thank you.
Is this in the documentation?
This should be added to the documentation for views. Very useful.