A Web Developer Looks at Drupal Views: Power and Flexibility

By - Tyme

In the last Drupal module review, I discussed the contributed module CCK. This time, I focus on Views – a module that puts real power and flexibility into your Drupal website.

Views pulls from your Drupal database a collection of data, which has been stored through creation of content, CCK fields; or stored by other modules. It then organizes and displays that content according to your specification using the Views data controls. For those of you familiar with SQL statements, that is what Views produces through GUI interface. You do not need to have experience writing SQL queries, but having that familiarity is an advantage for more advanced use.

This article is not meant to be a step-by-step guide, and the full capability of Views is beyond the scope of this article. However, I will touch on the basics in an attempt to excite you to learn and use the Views module.

Three examples of how you might use Views:

  1. To create a page of story summaries
  2. To display different types of data of the same subject, onto one page
  3. To create an interactive (user requested) display of content
When you build a Drupal site, you should plan for the possibilities rather than merely the immediate needs. One tool to help you exploit the opportunities, and make Views work to its fullest, is Drupal's "Taxonomy".

Taxonomy is made up of a parent "Vocabulary" and its children, "Terms". Another way to think about vocabulary/terms is as sections and categories (the Joomla equivalent). Taxonomy is essential for manipulating data in Views.

Story Summaries Page

A stories summary page is one of the most basic examples of a Views-generated page, and what many content management systems (CMS) would refer to as blog style – the familiar story teasers with a "read more" link to the full article. The Drupal default home page (referenced as <front>) does this automatically when a content item is enabled to "promote to front page".

In the above Overseas Press Club of America example, the menu shows that the News story type has been broken into categories: News Briefs, Features, and People Remembered (obituaries). Just as you would see in a print newspaper.

First, a taxonomy Vocabulary was created for "News" and under which Terms were created for "News Briefs", "Features", and 'People Remembered". With this organization, Views now can be instructed to display these individual collections of data onto separate pages.

Notice the right sidebar block "Latest News Briefs". This summary block was created in the same View, along with the full page of "Features" summaries.

Here's a look under the hood:

I have highlighted in red the key criteria that you will configure.

Fields

Remember, with Views, you are building a SQL query, so you must specify the fields that you want to pull from the database. Using the Views GUI interface, you simply click the plus sign (+) of the Fields box to display a list of fields, organized by type. In this case, you would use Node fields. Scroll through the list and check off the fields that you'd want to display.

In the News example, you see that I selected the typical story summary fields: story Title, story posted date, image thumbnail (a custom CCK field), and a story Teaser. [For more about CCK, read Drupal CCK: I Cee You.]

You may check multiple fields at once; Views will show additional settings for each, one screen at a time. These options will include teaser length, whether or not to have the title or image link to the full story, size for the image, and more.

Filters

Filters allow you to narrow the amount of data that you pull from the database, by selecting only certain records. These are the "WHERE" conditions of your SQL statement. In the above example, the type of content has been specified (Story), along with a directive to display only published content. You will notice also that I employed the all-important Taxonomy.

Because when creating my News content, I assigned each story a section (Vocabulary) and News category (Term), I am able to filter that data. And, with the Views interface, it is as easy as clicking the plus sign to display my options panel and then selecting the desired term. You can select to filter a term in (include) or out (exclude).

Page Title and Page URL

Lastly, you need to assign a page Title and a URL path for this web page – the way that you will directly access this Views-generated page.

Views Block

For the sidebar block, I added a display Block ("Add display"), which makes this View show up on the admin Block assignment page for positioning on the layout and among the site pages. Only a few changes are necessary to make the View more appropriate as a Block.

As shown on the live site screenshot, I chose to display News Features for the main content, with News Briefs for the sidebar block. So, for the filter, I pulled only news briefs. And, since it is a narrow sidebar, I reduced the number of items shown, the size of the teaser, and the size of the thumbnail image that accompanies each summary. I specified a "read more" link at the bottom of the block, and gave the block a thoughtful name so that I can easily find it on the admin Blocks assignment page.

On the same View setup, you can have a number of view types, the most common of which are page, block, and feed. The benefit of grouping them together on a View is that you can first set defaults – such as fields, node type, vocabulary/terms -- to apply to all, and then change minimal settings for the extra views.

Different Types Of Content On One Page

Using a different example: a school website. You might want to create a separate page for content related to each club at your school -- to include (for example) stories, events, announcements, featured member.

Most likely, you would have created each of those as a unique Content Type. In that case, you would follow the above method; for the filters, you would change the Node Type for each of your views.

So, for a typical Web page layout: on your Views setup, you would create a Page for the stories, a Block for the events, a Block for the announcements, and a Block for the featured member. Since each of those corresponds to a different Content Type, you would change the Filter "Node: Type" as appropriate.

After saving that View, you would go to the admin Blocks page and assign those Views Blocks to only your new Views Page for that club – using the path that you assigned for the Views Page.

User-Dictated Web Page

You can easily have a Views-generated Web page that allows the user to select what information appears on that page, based upon the established fields. An example of such a page is the Overseas Press Club of America's awards page.

This page has been constructed to allow the user to filter the awards archive by one or more of the fields: Award Year, Award Name, Award Type, Recipient Name, Recipient Affiliation.

You must be thinking that this is a very complicated procedure. Not so. One more option on your Views setup page and you'll have this interactivity.

Here is how that looks from behind the scenes:

When you set your Filter, simply click the Expose button to make that field a selection form field on the finished Views-generated page. That's it! Think of the possibilities.

Note: The fields prefixed with "Content:" are custom fields created with Drupal contributed module CCK. Don't forget to add that to your arsenal.

Summary

This overview has merely scratched the surface of the power of Views, especially coupled with CCK. But, I hope that it has piqued your interest and will send you rushing to http://drupal.org/project/views to download the latest release version.

Views is way too complex to have made it into Drupal 7 (very soon to be released). So, it remains a must-have contributed module in your Drupal tool box, and it is well worth the investment.


Tyme is a freelance writer and multimedia specialist of many years. She likes walks in the park, cotton candy, and baby ducks. To learn more: http://www.MultimediaByTyme.com/.

Make a Comment

Loading Comments...

  • Web Development Newsletter Signup

    Invalid email
    You have successfuly registered to our newsletter.
  •  
  •  
  •  
Thanks for your registration, follow us on our social networks to keep up-to-date