Book Image

Drupal Multimedia

Book Image

Drupal Multimedia

Overview of this book

Table of Contents (16 chapters)

Views


Views is another contributed module that is found on many Drupal sites. It is used to create custom listings of nodes that are filtered and sorted using unique criteria, and displayed in pages and/or blocks as needed.

These views may be replicated in other ways such as by using custom SQL queries. But most developers and administrators will probably find the administration forms used to build a view much easier to manage, particularly for complex arrangements.

A view can take data from many sources such as specific fields of nodes or user information, filter them for desired criteria, and sort them in nearly any order. The Views and other contributed modules expose several default views that may be activated and overridden on a site. For instance, you might override the default front page view to display only promoted nodes of an article content type.

Note

If you are familiar with Views from Drupal 5, you will be pleasantly surprised by Views 2 for Drupal 6. The engine has been rewritten from the ground up, allowing for new features such as creating views from data other than nodes. Additionally, the User Interface (UI) has been completely revamped using AJAX. This means the information you need when administering a view is all in one place, with a system of tabs and drawers to sort and access it, rather than the older system of collapsible field sets. If you are building a view for Drupal 5, the steps outlined here are largely the same, but the UI is considerably different.

View Administration

As with the Content module (and all other contributed modules), you will need to first upload the module directory into the /sites/all/modules folder. The Views module is available at http://drupal.org/project/views. Once present, you will activate it at Administer | Site building | Modules (at /admin/build/modules). On that page, you'll note that several other related modules are also available in the Views package. For the examples in this book, you will need at a minimum to activate Views and Views UI.

When you browse to Administer | Site building | Administer views (at /admin/build/views), you will see the system default views interspersed with any custom views you've created:

Note

If you have a lot of views on your site, you may control the order on this page with the drop-down selection filters at the top of the screen. Additionally, you may enter an optional View tag when initializing the view, which will allow you to sort them by functionality on the administrative screen.

Creating a New View

You can easily create a new view from scratch by clicking on the Add tab, which brings you to /admin/build/views/add. However, for this example, we're going to clone an existing view so that we can see how they're set up while building it.

Click on the Enable link to the far right of the frontpage view. This enables the view on your site. Then click on the Clone link associated with that view, which will show you how the front page is created by default. You will be brought to the Clone view frontpage page (at /admin/build/views/clone/frontpage ):

On the resultant screen, you'll be able to change the name and description of your new view and the tag, if desired. Although you can't change the type when cloning a view, when creating a view from scratch, you'll be able to select one of many View types such as Node, Comment, or User. This means you can create a view that is a listing of users just as easily as a listing of nodes or comments.

Change the name to articles_recent and describe the view appropriately. If you like, you can change the View tag to articles, to make sorting views easier in the future. Then click Next:

Basic Settings

On the left of the configuration screen, you'll see drawers for Defaults, Page, Feed, and some form elements to add new displays and analyze our view. By clicking on each drawer in turn you'll be able to see the different displays for our view, each of which can override the defaults to format a view in a specific way.

TheBasic settings section controls things such as the Title displayed for the view, the Style to display the data, for example, a List or Table, the number of Items per page, who can Access a particular view, and similar items. We'll leave these alone for now.

Using the Filters section on the right, we can specify which nodes will be returned for our view. In this particular case, we want to filter nodes by their type, as we only want to display the Article nodes we created earlier:

Press the "+" button next to Filters to add a new filter. This will show all available filters. We can select Node from the Groups selector to make it easier to find the Node: Type box, which we will select before pressing Add.

The section will be replaced with further options to specify how to filter the view. We will select our Article type and press Update here:

Next, click on the link for Node: Promoted to front page and then press the Remove button, as we want this view to retrieve all articles, regardless of whether they have been promoted.

Note

If you expose a filter, then it will also show up under Exposed Filters, where you may set further options. Exposed filters will be displayed at the top of a view, allowing the user to set those options to determine which nodes to display.

Finally, change the title (in the Basic settings) to Articles by clicking on None, entering the new title in the new text field below, and pressing Update.

Page Views

Now we'll set up the page. Click on the Page drawer to the left, just below Defaults. This section will determine how the view is selected and displayed as a page, in this case from /frontpage, as seen in the Page settings section:

Click on frontpage next to Path within Page settings, and change the path to articles. After you press Update, do the same for the feed by visiting the Feed drawer and changing the path from rss.xml to articles/rss.xml:

Then you can select Block from the selector above Add display, and press that button. This will create a new display for the view similar to Page and Feed. The only changes we need to make there are to set it to four items per page, rather than the default of 10, add a More link (which will allow users to click through to the page when there are more than four items), and to turn off the pager.

When doing each of these, we'll need to override the default values by clicking on the Override button that will appear in the information area (which will add the message Status: using overridden values above that area).

You may then Save the view, so we can see the results.

Advanced Views Options

The Arguments fieldset is used to create some flexibility from the URL. Thus, by setting the arguments here, you might have a gallery of all recently posted videos with a URL of /videos, and further display of only videos in a certain vocabulary with /videos/[vocabulary-id], and finally only videos in a certain taxonomy term with /videos/[vocabulary-id]/[term-id]. Note that if there are Arguments set for the view, then those will be combined with Filters when selecting the nodes to display.

Note

The Sort criteria will determine the order to display the selected nodes for the view. The frontpage and article views will first display nodes marked as sticky, and then set them in reverse order of creation time. We could easily change the order, for instance by sorting articles by popularity, author, or time of most recent comments.