Theming views in Drupal 7 can be very tedious to newbies. In the following example, a seemingly simple task has become an overwhelming research project into the innards of Drupal! Hoping an experienced drupalite can make quick sense of this.

In our example view we are outputing a product list to a page. One of the fields is a file type where the user uploads a PDF file. To gain maximum control of the layout, we were trying to utilize 'Rewrite the output of this field, and want to create a link to the PDF file with text inside of it that says Download PDF. Sounds simple!! However, so far it has not been.

Because this view is a list of a certain content type, node theming doesn't appear as one of the potential options for template theming. See bottom of the post for potential theming file name conventions that Drupal is offering up.

Also, this may be something easy to do with a hook in the theme's template file, but as of now, that is a little confusing as well and I'm not sure where to look for that solution.

So, for this post, we're hoping to get a clearer picture into the stock views management system. However, all suggestions are welcome!

Here is the code that resides in the rewrite section of the view for the Content-PDF field:

enter image description here

Here are the relevant Replacement Patterns:
[field_pdf_upload] == Content: PDF
[field_pdf_upload-fid] == Raw fid
[field_pdf_upload-display] == Raw display
[field_pdf_upload-description] == Raw description

The problem, [field_pdf_upload] - gives you a PDF icon, a link to the PDF and it names the title of the file. If I could figure out how to replace the file name inside the href tag with "Download PDF", then this would be essentially solved. I've tried many options in the replacement patterns and what not, but nothing has worked yet. I tried stripping HTML and then adding the direct path to the file and that worked, but if the upload has spaces in it, the path breaks.

If I had access to php in this section, I could do something like this

$fid = [field_pdf_upload];
$file = file_load($fid);

Maybe there is a way to do this and I'm just not getting it? I am going to start a separate jquery option thread for this in a second, bare w me and it will be written soon.

An alternate method would be to alter one of the theme suggestion templates listed under Advanced->Information - Below is the list of options.

Display output: views-view.tpl.php, views-view--product-landing.tpl.php, views-view--page.tpl.php, views-view--product-landing--page.tpl.php, views-view--default.tpl.php, views-view--default.tpl.php, views-view--page-1.tpl.php, views-view--product-landing--page-1.tpl.php

Style output: views-view-unformatted.tpl.php, views-view-unformatted--product-landing.tpl.php, views-view-unformatted--page.tpl.php, views-view-unformatted--product-landing--page.tpl.php, views-view-unformatted--default.tpl.php, views-view-unformatted--default.tpl.php, views-view-unformatted--page-1.tpl.php, views-view-unformatted--product-landing--page-1.tpl.php

Row style output: views-view-fields.tpl.php, views-view-fields--product-landing.tpl.php, views-view-fields--page.tpl.php, views-view-fields--product-landing--page.tpl.php, views-view-fields--default.tpl.php, views-view-fields--default.tpl.php, views-view-fields--page-1.tpl.php, views-view-fields--product-landing--page-1.tpl.php

Field Content: PDF (ID: field_pdf_upload): views-view-field.tpl.php, views-view-field--field-pdf-upload.tpl.php, views-view-field--product-landing.tpl.php, views-view-field--product-landing--field-pdf-upload.tpl.php, views-view-field--page.tpl.php, views-view-field--page--field-pdf-upload.tpl.php, views-view-field--product-landing--page.tpl.php, views-view-field--product-landing--page--field-pdf-upload.tpl.php, views-view-field--page-1.tpl.php, views-view-field--page-1--field-pdf-upload.tpl.php, views-view-field--product-landing--page-1.tpl.php, views-view-field--product-landing--page-1--field-pdf-upload.tpl.php

Can anyone recommend a method and sample code for accomplishing this task?

1 Answer 1


You don't need to use a template or get your hands dirty with code.

Just add the field to the view twice. Hide the first field from display, but change the formatter to "URL to file". Rewrite the output of the second field to "Download PDF". On the second field, select output field as link and assuming your field is named 'pdf', make the path [field_pdf].

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.