Spotlight Views Module

The Views module provides listings of data on your site: users, comments, nodes, and more. Any listing of data provided by the Views module is called a view, which we'll always refer to in all lowercase to distinguish it from the Views module, which is capitalized. Figure 3-17 shows examples of some of the listings that can be built with the Views module.

Figure 3-17. Examples of views created by the Views module

Creating a basic view entails selecting the fields you would like displayed (node title, author name, and images, etc.), how you would like that list to be filtered (only display "story" node types that are published), how you would like the listing to be sorted (newest stories on top), and what you would like the list to look like when it's displayed (a block showing a bulleted list of headlines).

In more technical terms, Views is a visual SQL query builder. When you build a view, you are essentially constructing a query that Views will pull from your site database. The Views module has significant advantages over a handcoded query. Some examples:

• You don't have to write any code just to make a listing of content.

• Modules will tell Views about their fields; you don't need to know anything about the underlying database structure, and you are insulated in case this structure should change behind the scenes between module updates.

• The same view can be used in several places on the site, as both blocks and pages.

• Results can be split into multiple-page listings, use sortable table columns, AJAX pagers, or filtering drop-downs to allow visitors to "drill down" to the content they want.

More than anything else, Views can significantly speed up the development of your site, without your having to learn module development or a single line of PHP. Views can form the backbone of outputting content on your site.

SQL and Views

SQL is a computer database language that allows for retrieval of data from a database. SQL is made up of simple commands such as:

SELECT title FROM node WHERE nid = 10

Each of these commands is called a query. These queries can get quite a bit longer in order to retrieve the necessary information from the database, but that's one of the reasons Views is so helpful: it can build the queries for you.

Because a view is based upon a SQL query, many of the concepts in Views map directly to SQL. Consider the basic parts of a query: the select statement, where clause, and order by clause. These map directly to fields, filters, sort criteria, and other views components covered later in this chapter.

SELECT [fields]

FROM [view type and any relationships] WHERE [filters or arguments] ORDER BY [sort criteria]

Although you don't need to know SQL to use Views, the correlation is very strong and might help you to understand Views more easily if you're familiar with SQL or are converting existing code to views.

Unlike the configuration of a new content type, the creation of a view is done entirely on a single page. Figure 3-18 depicts the main portion of the view-building interface.

0 0

Post a comment