Quick Tutorial on Overriding Drupal Views using Templates

Chris Armstrong
Chris Armstrong
27
Feb 2013

Quick Tutorial on Overriding Drupal Views using Templates

comment icon5 comment(s) |

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 "Theme: Information" 

    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 "<?php" and "?>") 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.

Comments

February 27, 2013

Anonymous

Isn't it easier to use View

Isn't it easier to use View Modes on entities, and specify Rendered Entity as the format of the Views output? From there, you can create a simple tpl file for the View Mode (entity--type--view-mode-name.tpl.php).

February 28, 2013

carmstrong

First off, thanks for

First off, thanks for reading!

I'm not sure I would say that it is easier, but looks like it can certainly be done that way. I'm explaining how to use Views' built-in overrides, since I'm creating a view, then a tpl (whereas, it looks like you're using an extra module, creating a view, and then creating a tpl). I'll give you that the names of the Views tpls are definitley less predictable, but they also allow you to override multiple views using the less specific names. If you compare the override names on, for example, the title field of a block and a page, you'll notice that several of the names are the same. The farther down the list you get, the more the names diverge. But if someone is already using View Modes, then, yes, it probably would be easier.

August 30, 2013

Anonymous

view modes are in core

Nice tutorial, v. much needed.

view modes is not a different module, it is a drupal core feature that enables us to tweak the layout and structure of an instance of an entity type (i.e; a node page). you are already using them (Default, Full Content, Teaser, RSS,..) and can be used in views, as noted by your reader/commentator.

May 12, 2014

faizal

Thanks You SO much for this article

YOu Saved my day!

August 6, 2014

JosephRae

Thank You!

Thank you. Thank you. Thank you...!

... did I mention, thank you?

You've just helped me solve a hair pulling endeavor of applying Omega's equal-height columns to rows in a views display... and now I can finally get rid of those unresponsive table grids that were displaying my porfolio items.

Thank you, BTW.

Search