The Power Of Drupal Views

 When people ask me why I would ever use Drupal to build a website instead of other platforms like WordPress or Movable Type I typically have one answer, "CCK and Views." That could be interpreted as two answers but since the modules go hand in hand I'll call it one. I've written about the basics of CCK before so I recommend that you check out that post if you're interested in understanding CCK better. This post is about the Views module.

Views is a module that offers Drupal site developers (you I presume)  great flexibility of choices for displaying content. Views provides this flexibility by offering up a graphical user interface (pictured below) that allows you to query the Drupal database for content and choose the format of the content display without having to write SQL queries. I've written plenty of SQL queries in the past for business purposes and if I never write another one I won't be disappointed. If you want a much closer look at the interface I'm speaking of feel free to check out one of my videos that shows you how to do something practical with views.

Views Interface

Views Interface (Drupal 6)

Three Basic Types of Views

There are three basic types of views available in the base module. You can display content as a page, block, or RSS feed. When I say "content" I mean entire Drupal nodes or individual fields. For example, the page at http://learnbythedrop.com/cckandviews is a "view" that lists x number of nodes. When you list the node you get the Title + Body, plus links to comments and any other fields that would display when you are viewing a node. If you choose to display fields instead of nodes, then you can pick and choose individual fields from your nodes to display. For example,. the page at http://learnbythedrop.com/archives displays the Title (linked to the node), (Number of) Views, (Number of) Comments and (Number of) Votes fields.

The one thing that the views mentioned in this paragraph have in common is the fact that they are "page" views. Page views differ from other views because you assign a path (like http://learnbythedrop.com/archives) that displays the view as a page on your site. You can also create your list in a "block" view. When you choose block the content that you add to the view will be available on the block page (at admin/build/block) for configuration and placement just like any other block on the site.

You should put a little bit of thought in to what types of content you display in a block vs. a page. Blocks work well with lists of titles linked to nodes (see the "Recent Drops" and "Comments" blocks in the right sidebar), whereas a page might work best with full nodes or a collection of fields. Lucky for you that the views module has a very handy "live preview" feature that allows you view the results of your query while building your view.

The third type of standard view is the RSS feed view. This view outputs lists of nodes as an RSS feed that people can subscribe to in a feed reader such as Google Reader. If you have set up a page view that has the same content as the RSS feed you have the option to associate the RSS feed with that page and a small orange RSS Icon will appear on the page. See the bottom of the page at http://learnbythedrop.com/gallery for an example of how this looks.
 

Style Options

Drupal views also have several standard "style" options which differ depending on the type of view you choose. RSS, for example, is really a style option for your view. But if you choose to set up a page or a block view you have different options as to how the final display will appear. Some examples of style options are unformatted, list, grid and table. If you're creating a page full of nodes then you'll likely choose "unformatted" as your style. But if you're choosing a page with individual fields (like http://learnbythedrop.com/archives) then a "table" style might work best. For blocks that have lists of titles linked to nodes I typically choose the "list" style. Once again, the live preview option is your friend when making these types of decisions.

The "grid" style is an interesting choice if you're displaying just a couple of fields and want to repeat the content across and down the page. I've implemented this style on my view at http://iheartmets.com/metstweets, which shows a grid of recent tweets about the New York Mets.

<!--break-->

Display Controls

Drupal views provides very good control over exactly what and how content is displayed. The "filters" section allows you to indicate the rules for displaying content. For example you can choose to display only a certain node type. Or you can only display posts from a particular category. You can mix and match the controls to select, for example, only blog nodes with a category of "dogs". You can also choose to only display nodes that have been published, which would exclude posts that have been saved as drafts. That's probably a good idea.

You can also control the sorting of your content. You can sort based on any number of fields. For most things, but not always, I choose to sort based on the post date, descending so that the newest content shows up at the top of the list. You can also choose to display content in the order that things were updated so every time there's an update that content moves to the top of the list.If you are creating a view that is displayed in a table you can also make each of the headings sortable to the user as I have done over on http://learnbythedrop.com/archives.

Additional Features

While the views module has many valuable features that come standard you may also want to explore some of the other modules that extend the features of Drupal views. I've listed some of my favorites below.

  • Views Slideshow allows you to display the items in your view as a slideshow. This works great with photos (see this example) but will also work with text as well. I've got a pair of views slideshows running on the front page of http://iheartmets.com. The views show a rotating lists of tweets and podcasts.
  • Views Accordion allows you to display content that opens up when the header is clicked. I'm using this module on the page at http://learnbythedrop.com/gettingstarted.
  • Media RSS is a very useful module for image publishers. It adds social tags that allow the photos you publish to be easily picked up by subscribers and other websites that import your feed. I'm using this module on my image gallery feed at http://learnbythedrop.com/gallery/feed.
  • iTunes lets you publish iTunes compliant podcast feeds, which provides a homegrown alternative to using a service like FeedBurner.I'm not using this one right now but I am working with it on a test site.

There are also many other modules that add options which allow you to do more with views. For example when you add a rating module, like Plus 1 for example, you get the option to access the voting data via views and present that data as I've done on http://learnbythedrop.com/archives. If you want to browse and keep up with all of the possibilities for adding on to views I suggest that you check out the views module category on Drupal.org.

Learning More

There's a lot to the views functionality and it's a very important part of any Drupal site that seeks to move beyond very basic publishing. While you can build a pretty good site without using views there will likely come a time when you want to see something displayed differently. And when that time comes you don't want to be fooling with SQL queries in order to make it happen. So I recommend that you first become a student of the basics of views (and it's companion module CCK), trying out the features on a test site until you feel confident to start working on a production site. The time you put in will be well worth the effort.

The team at Lullabot has a views video product that may be able to help and they have posted  a pair of free chapters so you can get a taste of what they have to offer. I've got my CCK and Views page (created with views of course) which offers you some more learning as well.

Good luck and please let me know your thoughts, comments and questions about the views module in the comments section.

Copyright 2009, Robert Safuto, Some Rights Reserved. Please visit Learn By The Drop to comment, subscribe or explore additional content not available via the RSS Feed.

Comments

Views from Fields

You mention making Views to display Fields ("If you choose to display fields instead of nodes,"), and I've run into a problem with it that I'd like to hear from you on!

I have a custom content type "Song" with a custom field "Genre" that is populated by a select box. How do I make a view that just shows what the Genres are? I tried this: I made a view to display nodes, and selected the Genre field as the only field to display.

The problem is that the genre appears once for every Song that's been labeled with that Genre! So I get a lot of "Rock," when I just want it to appear once. How can I create a view that outputs my list of Genres?

Thank you so much, and great article!

- Lems