Quick Tutorial on Overriding Drupal Views using Templates

Dev

Quick Tutorial on Overriding Drupal Views using Templates

by Chris Armstrong

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 About the Author

Chris Armstrong

Multimedia Manager
Drupal Quick Tip: Auto “node/nid” Paths from Alias in Menus “In learning you will teach, and in teaching you will learn.” Words spoken by (or, at least, attributed to) the great Phil ...
Quick Tutorial on Overriding Drupal Views using Templates Scott and I were recently invited to Oklahoma State University to provide Drupal training for their web admins. Drupal has been on the ...

See more from this author →

Subscribe to our newsletter

  • I understand that InterWorks will use the data provided for the purpose of communication and the administration my request. InterWorks will never disclose or sell any personal data except where required to do so by law. Finally, I understand that future communications related topics and events may be sent from InterWorks, but I can opt-out at any time.
  • This field is for validation purposes and should be left unchanged.

InterWorks uses cookies to allow us to better understand how the site is used. By continuing to use this site, you consent to this policy. Review Policy OK

×

Interworks GmbH
Ratinger Straße 9
40213 Düsseldorf
Germany
Geschäftsführer: Mel Stephenson

Kontaktaufnahme: markus@interworks.eu
Telefon: +49 (0)211 5408 5301

Amtsgericht Düsseldorf HRB 79752
UstldNr: DE 313 353 072