How to Drupal: Views Module
Though Views module is not standard Drupal module, it is one of the basic modules. I install Views every time creating a website even if I’m not going to use it so far. I’m sure that first or last I will need Views!
In this article I would tell you about Views module and describe how you can work with it on the example of creating a block that would display the last articles added to your site.
The Views module provides a flexible method for Drupal site designers to control how lists and tables of content (nodes in Views 1, almost anything in Views 2) are presented. Traditionally, Drupal has hard-coded most of this, particularly in how taxonomy and tracker lists are formatted.
This tool is essentially a smart query builder that, given enough information, can build the proper query, execute it, and display the results. It has four modes, plus a special mode, and provides an impressive amount of functionality from these modes.
Among other things, Views can be used to generate reports, create summaries, and display collections of images and other content.
What the official documentation tells about Views?
You need Views if:
- You like the default front page view, but you find you want to sort it differently.
- You like the default taxonomy/term view, but you find you want to sort it differently; for example, alphabetically.
- You use /tracker, but you want to restrict it to posts of a certain type.
- You like the idea of the ‘article’ module, but it doesn’t display articles the way you like.
- You want a way to display a block with the 5 most recent posts of some particular type.
- You want to provide ‘unread forum posts’.
- You want a monthly archive similar to the typical Movable Type/Wordpress archives that displays a link to the in the form of "Month, YYYY (X)" where X is the number of posts that month, and displays them in a block. The links lead to a simple list of posts for that month.
Views can do a lot more than that, but those are some of the obvious uses of Views.
So, if you wish to use Views for your Drupal web-site, download it and install. You can find the detailed instruction of how to install Drupal modules here: http://www.templatezine.com/2010/01/how-to-install-a-new-module-to-drupal-based-site/. Enable all components for this module.
Let’s proceed. Now we are going to create a block that would show the last 5 articles added to the website.
Navigate to the Administer > Site building > Views item of the admin menu:
And then click Add tab:
Fill in the details of our block:
Please make sure that you’ve selected Node for the View type.
You will then see the Latest News settings:
Add fields for your block with the latest articles.
Fields are only applicable to a view if either the page or block type is set to List or Table view. If using a purely teaser view, fields will be ignored. Fields tell Views exactly what to display and in what order.
Click “+” button within the Fields area and add Node: Title type of the field for your block:
Check the Node: Title from the list of the available fields and click Add button at the bottom of the page. Configure this field:
Check Link this field to its node box and click Update.
Add Filters for your block.
Filters are what are used to reduce the result set of a View to a manageable amount of data. They are a critical part of the foundation of this system.
The filtering system is a compromise between flexibility and ease of use. To be truly powerful, filters should provide the ability to do an OR, but as it stands, these filters are all ANDed together. That means that every filter is applied. While the ability to do an OR would be nice, unfortunately the level of complexity in the interface to do that is well beyond the benefit that would be given.
The order of filters is irrelevant, though they can be re-ordered as a convenience.
Then specify a filter for your block. Click “+” button within Filter area and add Node: Type filter:
Configure filter Node: Type:
Check Page as a Node type and click Update.
Then add Node: Published filter and select Published – Yes when configuring it.
Add Sort Criteria for your block.
The sort criteria on a View controls the order in which views results are presented. In general, sort criteria are specified by a direction, and the order is important.
Click “+” button within the Sort Criteria and check Node: Post date item:
Configure the selected criteria in the following manner:
And click Update.
Change Items to display from the default 10 to 5 items within Basic settings:
Select a Block View for your block.
A block view will appear as a ‘block’ on your site, meaning it can be put into the sidebars, or any other region your theme supports. The same view can provide both ‘block’ and ‘page’ views and they can have very different characteristics.
Set Defaults to Block:
And click Save button at the bottom of the page. We’ve done! Now you block appears in the right-side area of the page.
However, you can change allocation of any block (read more: http://www.templatezine.com/2010/01/how-to-allocate-blocks-on-your-drupal-based-web-site/)