Drupal Templating Tricks
So I’ve been working on our Intranet redesign with Drupal and one issue we have is we want people to be able to post PDFs to the Intranet. The simplest way to handle this in Drupal is to create a content type for PDF and use the CCK file field to give people the ability to upload files. This way the PDFs sit by themselves and won’t have to be attached to a particular page (which is what happens if you use the default Drupal upload module). Also the upload module makes it so that any node can have files attached which I didn’t want.
The issue with doing it this way is that when you click on a PDF node you end up at a page that has the Title and a link to the PDF you uploaded. This isn’t what we wanted. Instead we wanted the link to take us to the PDF proper. To do this one has to make a template for that content type. Normally when you want to alter the template for a content type you create a new node template. But this only allows you to alter the content portion of the screen (ie. the portion where the node displays usually the center of the screen). If you want to change something more than this you need to alter both the page and node template along with adding some new info to your template.php file. The basic steps are as follows:
- Alter template.php to add code for your new template. It will be something like the following
function my_theme_preprocess_page(&$variables) {
if ($variables['node']->type == ‘my_content_type’ && arg(2)!=’edit’ && arg(1) !=’add’) {
$variables['template_files'][] = ‘page-node-my_content_type’;
}
} - Create a page-your_content_type.tpl.php file
Put into this file changes to the sections of the page other than the main content area - Create a node-your_content_type.tpl.php file
Put changes here to how you want the content to display. The default node.tpl.php just drops in all the content but you can selectively show certain fields and govern how you want those fields to be displayed. - Clear your cache
Site Configuration | Performance
The key part of this whole process was discovering the drupal_goto function which basically is a way to do a redirect within Drupal. The advantage over a traditional PHP redirect is that you can go to a full url or an internal Drupal url like node/4. So my node-pdf.tpl.php file has the following in it
drupal_goto($field_pdf[0]['filepath']);
Now my PDFs nodes all redirect to the actual PDF if you are viewing the node. If you edit the node then you get the normal edit form.
Some key Drupal documentation that really helped me with this
- Core templates and Suggestions – really important to get to information about which variable are available to use in which template files
- Working with Template Suggestions – Read both the page and the comments which have good information
Hi Karen,
Since you mention doing a redesign with Drupal, was there something already in place (Wiki, CMS system) that had problems that lead to moving to Drupal?
Our intranet was a hodgepodge of some blogs, some wikis and a very very old set of HTML pages. Also, we send lots and lots of stuff out as emails to the entire library, often with attachments. Noone knew where to look for information. The new intranet is meant to centralize things, provide a single interface and make things easier to update. We’re still moving towards all that providing training for staff and consolidating old systems and content.
First, thanks for sharing this code. It has really helped me in overcoming an issue that I had with making PDF documents available on our Church website. I am currently re-working the site and this works much better than the old method I had been using with webfm. I may be missing one thing however and thought you might know what that is. I followed your instructions and I can get pdf’s to display now instead of opening the node page and having a download link and that is what I want. My problem is that when I upload the files through a create content dialog that uses the filefield module, when I upload the PDF it always wants to save or open the pdf instead of just uploading and creating the new node. I am sure I am overlooking something simple. Thanks again for sharing this. I am still fairly new when it comes to Drupal but am really excited about what I can accomplish with it and am learning more all the time.
I’m not sure there is a way to prevent this. At least not that I’ve figured out yet. When you save the node it returns you to ‘view’ mode which means that it will show you the PDF. When I use CCK and filefield for this and click the upload button the file gets attached. When I save it though I end up at the PDF. Another possible option is the iPaper module (http://drupal.org/project/ipaper) which allows you to upload PDFs to Scribd and easily display them within the site. Granted you need a Scribd account (free) and your files are stored on their server but it is a possibility. Also another advantage is that you can upload other file formats and it will convert that into a format that can easily be displayed on the web.
You might also consider adding an if statement that checks to see someone is logged in and if they are not forwarding them. But that assumes that logged in people don’t want to go directly to the PDFs.
I appreciate that code that you provided earlier but i wanted to let you know that I recently found a way to get around this issue that also does not result in a call to open the PDF when you upload it. There is no coding involved and it just uses built in views functionality. I am using views 2 under Drupal 6.
I create a standard view that will list the nodes that I want.
I start by selecting fields and adding Node:Title. The only box we want selected under node title is exclude from display. Click on update and ignore the warning about not having any fields defined and go on to add another field.
The second field to be added is Content: PDF (field_pdf). This is from my custom content type.
Now go on to configure this field.
Select rewrite output of this field and put [title] in the text box for what you want it to dynamically produce.
Next Select Output this field as a link and enter [field_pdf_fid] in the link path box.
Select None for Label
From the Format Drop Down list select Path to file. and hit update and save.
Now when you select the link that was produced you get a file dialog to open or save the file instead of getting redirected to the node page that displayed the file attachement.