Quick Tutorial on Overriding Drupal Views using Templates

Scott training at OSU

Scott and I were recently invited to Oklahoma State University to provide Drupal training for their web admins. Drupal has been on the main campus for a couple of months now and is creeping its way onto the extension campuses. Web admins from each campus - Oklahoma City, Tulsa, and Muskogee - and every department were invited to participate. The open-forum format allowed for specific questions to be asked and answered while allowing for all others to benefit.

We covered several topics including Nodequeues, Feed Import, the Menu system, and Views.

I consider Views a system within a system. While Views really only does a handful of things - aggregate content - the flexibility of it is incredible. When learning Drupal, it's difficult not to encounter a situation where Views would come in handy. And while Views does give users a GUI to it's SQL-writing underpinnings, each view, row, or field can be overridden using template files.

One great thing about this is that each View will give you the correct name and template for each item you may want to override. To do this, go into the view and:

  1. Expand the Advanced column
  2. Click on the "Theme: Information" link. This will bring up another overlay that will let you select a specific template.

    Expand Advanced column and click on  

    Select the template.

    NOTE: Views checks these names sequentially for overrides. Names farther down the list will be used before names listed first. InterWorks' process is to use the very last name, so that it's the most specific and can't be overridden.

  3. Click on the title of the template that you'd like to override and copy the template (everything between the "") and paste it into your editor.

    Copy the template code

  4. Edit the file as needed
  5. Save the file in your templates directory and upload

    Save file and place in your theme's template directory  

To confirm that your template is going to be used by the view, click on the "Rescan template files" button. If the name in bold changes to the one you created, all is good. If not, double-check that the file has been named correctly and uploaded to the correct directory - whenever I have issues with the view not seeing the new template files, it is one of those two things about 90% of the time.

Confirm the override

But before you start overriding each of your fields and rows, try to use the Views GUI. A lot can be done with Contextual Filters and Relationships. However, for certain situations, like when you need to group multiple fields into a specific format, overriding the default view template is one of the easiest and most straight-forward ways to get the job done.

More from the Author