Want to get Lullabot article, videocast, and podcast announcements delivered right to your in-box?
Let us know your email address (we won't share it) and we'll let you know when anything exciting happens.
Hacking Views, Part 1: Basic Concepts
The 'Views' module is one of the mainstays of Drupal site building. It allows non-programmers to build highly customized listings of data that match certain criteria, then present that data in a variety of ways. A thumbnail gallery of photos, an alphabetized listing of site contributors, and a calendar display of upcoming events are all common applications of Views.
In this series of articles, we'll be taking a quick look at the architecture of the Views module and how its pieces work together; touring the different plug-in points that Views offers developers; and building a simple 'argument handler' for Views that demonstrates how the approach looks in the real world. A bit of knowledge about SQL will be useful for the article, as well as some understanding of object-oriented programming concepts like 'inheritance', but the code samples should be simple enough to tweak even if you're not a pro.
Under the hood, the pieces of a View can be divided into two different groups: the 'data' (stuff that affects the underlying database query that Drupal uses to retrieve the information for the View) and the 'presentation' (stuff that affects how that data is displayed to a user of the web site).

Buildin' SQL
The 'data' portions of the View are more numerous: they correspond roughly to the different pieces of a SQL 'SELECT' statement. That should come as no surprise -- at Views heart is a SQL query builder that turns all of your settings into a query against Drupal's database tables.
- 'Base Tables', like Node or Comment or User, are the underlying kind of data that you'll be displaying. They correspond to the main database table in a query, and a given view can only have one of them. A view of 'nodes and users' would result in monstrously complex SQL, and Views doesn't attempt to solve that particular problem.
- 'Fields' don't appear in every view: they correspond to the individual database columns in a SELECT query. If you're building a table of data, for example, you'd add one Field to the view for each column that you need to display. Some complex Views fields can correspond to multiple database columns -- the 'Node teaser' field, for example, requires both the 'teaser' and 'format' database columns in order to be displayed properly.
- 'Filters' correspond to the WHERE clauses in a SQL query. They filter down the giant pool of data in your Drupal database to a more manageable set. When building Views of nodes, for example, it's common to add a 'Published' filter to prevent unpublished or in-progress posts from appearing. Filters that restrict the results to nodes of a particular type, or nodes posted within a certain date range, are also common. Generally, whenever a Field is available for a given Base Table, a corresponding Filter is also available.
- 'Sorts' are straightfoward -- like Filters, they generally correspond to the existing Fields for a given Base Table. They change the order in which the final results will appear, and they correspond to SQL 'ORDER BY' clauses.
- 'Arguments' in Views are really a special case of Filters: they restrict what will appear in a given View. Hoewver, the specific value an Argument uses to filter the results can change based on the context in which the View is displayed. Adding a 'User ID' argument to a view living at http://example.com/blog, for example, would cause Views to look for a User ID after the word '/blog' in the current URL. http://example.com/blog/1 would show blog posts by User 1, http://example.com/blog/2 would show posts by User 2, and so on.
- 'Relationships' are not used as frequently, but are still important. When two Fields (or Sorts, Filters, etc) are selected that correspond to columns in different tables, Views is smart enough to construct JOINs between the tables automatically. Sometimes, though, a query is too ambiguous for Views to automatically build the connecting SQL. In those situations, defining a 'Relationship' for the view makes the correct connection between the two tables explicit. One example is a view that shows the titles of two nodes that are connected to each other via a Node Reference. You can add two 'Title' fields to the View, but you'll need to add an explicit Relationship to the View to let it know which of the nodes each Title field should come from.

Using those six building blocks, the Views module is able to construct most SQL SELECT queries. Modules that maintain database tables can use hook_views_data() to announce what base tables, fields, sorts, filters, arguments, and relationships they offer, and Views will automatically add those options to the View-building UI.
Making it Pretty
While the Data portion of Views has what's needed to generate a SQL query, that only gets us part of the way. How should the resulting data be formatted into HTML and presented to someone visiting the site? For that matter, where should the information be displayed -- a sidebar block, a dedicated page, or perhaps an RSS feed? That's where the 'presentation' side of Views comes in.
- 'Displays' are responsible for tying Views into the flow of a Drupal site: they control when a View's SQL query will actually be executed and how the information will ultimately be woven into the site's structure. Page displays are like any other Drupal page -- they show a View's contents at a particular URL, and can have an entry in the navigation menu. Block displays display a View's contents in a sidebar block, and Feed displays expose Views data as RSS feeds. Custom Display types are provided by some third-party modules: 'Views Attach,' for example, allows a View's data to be attached to the User Profile page or the Node view page. Every View can have multiple displays -- allowing the 'Latest blog posts' data to be reused as a block, a page, an RSS feed, and so on.
- 'Styles' control the appearance of a View, wrapping the data that's returned from the database in HTML markup and other presentation goodness. Tables, grid layouts, unordered lists, and so on are all examples of different View styles. More complex Styles can present data in Javascript-powered slideshows, raw XML, collapsing blocks, and so on.
- Some styles also rely on 'Row Styles' -- an additional component that handles formatting each row of data returned by the SQL query. The most common Row Style -- 'Fields' -- prints out each of the fields defined in the View's data settings. It's used when building lists of links or tables of data. The 'Node' row style, in contrast, ignores the Fields that were set up on the View and simply loads the entire node object, displaying its teaser. It's often used to create variations on Drupal's 'river of news' presentation.

Next Time... Adventures With Plugins
Whew. All that, and we've only just begun to scratch the surface! In the next installment, we're going to take a look at how Views' object-oriented architecture implements all of the pieces we've discussed, and how it provides points for customizing through its plugin architecture.
Comments on this post will automatically be closed three months from the original post date.



RSS Feed



Comments
Thanks
That's really great! Looking forward to part II.
nice
nice article !
looking forward to part 2 of the article.
Can plugins be combined with CCK
Hello,
I too am looking forward to part 2 of this article. As a little preview can plugins interact with CCK fields and such? We are using a bunch of CCK stuff for the e-commerce interface to our Handmade bags site. Look forward to the next article - thanks for all the insight.
Thank you, useful article. I
Thank you, useful article.
I think on the picture of the SQL query the word "Argument" may point on "1", perhaps it will be easier for some people to understand.
Yeah
Sittin' back, rockin' a little... smilin'... drummin' fingers, waitin' for part two here.
Excellent article
I'm looking forward to the rest of the series.
Thanks
Thanks Jeff. Good Stuff!!
Sounds like it is going to be a good series.
Can't wait for part 2 of the
Can't wait for part 2 of the article..Thnx jeff
very nice post
Very nice post!! waiting for second part :)
much needed - some older Views 2 tutorials are out of date now
Trying to find good views2 tutorials so this is very helpful.
If you scour the web for Views2 tutorials, it appears those tutorials are obsolete when it comes to issues like writing custom handlers or hooking into non-Drupal tables in MySQL. This raises the learning curve considerably.
Even the Orielly book is out of date in some places (published Dec 2008!), so yes... this is awesome, thanks! Looking forward to more advanced Views2 articles.
YES!
Holy crap, I am super excited for this series. Thanks!
Perfect timing
I am speaking about Views at DrupalCamp Dallas this weekend :)
Waiting for Styling...
A great introduction for Views, indeed.
I wish the next chapters will deal with styling a view with CSS.
Thanks Jeff!
Great stuff
I had difficulty understanding the Relationships part in views but this article has helped a lot. The other explanations that I went through for Relationships do not delve into the backend of how views, and especially Relationships, work while Jeff has really nailed it.
Thanks for such a great article.
I like it!
Thanks for the article, looking forward to reading more!
A good start
Looking forward to more - this is a good basic introduction to Views - looking to learn more about it's innards. More documentation for Views2 is good!
Part 2, and maybe 3 and 4?
I'm looking forward to the continuation of this, since this is just an overview.
We've done a few things with plug-ins, like defined relationships for a few grandparent relationships, and a small bit of displays work. It seemed to be much harder to do than it should be, likely indicating these are areas that could benefit from improved documentation. To take another example, I wanted to enhance the Edit View module to handle more cases (eg data from CiviCRM tables), but I found the row-style stuff a bit daunting. There is so much opportunity here to enhance Drupal as an application development framework, but the learning curve is pretty steep.
Thanks for your work on this - it's much appreciated.
Yep, those parts are coming.
The second installment was delayed a bit, but it's an overview of Views' object model and how the different classes interact as a View is being designed. That segues into the third part, how to write your own custom plugin classes.
nice
Nice article... a good appetiser for part two!
Thanks!
Oh wow, thank you for explaining it in SQL terms, I can't believe how easy it was to understand!
Looking forward to this series, good job!
Views never tasted this
Views never tasted this better! You're a great chef (errr, writer) Eaton.
I can't wait for Part 2!
hacking?
the title of this article is scaring newbies away, as they go "I don't want to hack views"
but this is actually a very good basic explanation of how views works, so don't get confused by the title, no scary stuff here ... (yet)
Did I miss somthing?
Great tute! I think you may have left off the very first part of creating a view that still questions me to this day. Upon creating a new view, I'm asked what kind of view this will be. I always choose NODE, but what are those pesky other types of views? Gotta know... thanks!
Go Eaton!
Ohh, man, if you had the next two parts posted right now my day at work would be a bunch easier. I'm still looking forward to the next articles, but I guess I'll have to do some views plugins research myself today....
looking forward to part 2
looking forward to part 2