Views D6

by Gisle Hannemyr

This is the legacy version of the Views Intro chapter. Just a reminder until I've transferred all the stuff to the new version.

Table of contents

Drupal projects discussed in this chapter: Views.

Introduction

This is stuff left over from the D6 chapter that dodn't make it into the D7 chapter..

Creating the first view

Display

To define a display for this particular view, do as follows.

The drop-down menu in column one lets you select what type of display to add. First, you should add a page display. Select Page from the menu, and click Add display. This creates a page display for the view.

So far we've been working on the default settings. Displays, such as Page automatically inherit all default settings until we override them. But there are some settings that can only be set in the Page display, as they do not have a meaning outside of the Page display. One such setting is Path.

To specify a path, switch to the page display by clicking on Page in column one. Look under Page settings in column two. Currently, no path for the page display has been defined, and we need to do that. Click on None to the right of Path:. This brings up a panel that let us specify the path to use for this page display. You should just type “blogs” into the text field, and click Update.

Specify path

You now should have a view with a valid page display.

Legacy (?) buttons

There are also some buttons. The +-button lets you add elements to a setting, the v^-button lets you rearrange the order of a setting, and the o-button lets you change settings. In some panels there will also be a ø-button to remove items,

Re-arrange

Next, we should rearrange the order the fields are presented. You open up the panel to rearrange fields by clicking on the v^-button in the Fields panel. Use drag&drop to arrange the fields in the following order.

Rearrange fields

Title

The first entry under Basic Settings is Name. This is the name that will be used to identify the display in the administrative interface. For the defaults display, you should just use Defaults.

The first thing to do is to give the view a title. The title is just a piece of text that will appear whenever the view is shown – usually above the view contents. The title control is in the second column, under Basic Settings. You activate it by clicking on the current title (None if the view is brand new) that is coloured blue to indicate that it is a link. This brings up a panel where you can type in a title.

Note the prefix Defaults in the panel. This tells you that you are changing settings for the default display.

View title

Type in the string you want to appear as title for the view (e.g. “Recent blog pages”), and click Update.

noteClicking Update does not save the view. It just updates the settings within the view editor. To save any changes you have made to a view, you must click on Save.

Format

At this point, you would in most cases select a Style for the view. The Style setting is under Basic settings in column two, and is by default set to Unformatted. However, we going to leave the view unformatted for now, and return to the Style setting later in the tutorial.

A bit further down under Basic Settings is something called Row style. Click on the current value to open up the following panel:

Row style

A view is a list of contents, and Row style is used to specify what each row in the list is going to contain. There are two options: Fields and Node. The Node style means that each row will contain a copy of the full node. The Field style lets the designer specify which individual fields to show. Pick the Field style.

Sort criteria

The view of the blog entries should be presented in chronological fashion, with the most recent entries first. To make this happen, you should add a sort criterion. This, by the way, is the Views counterpart of the SQL ORDER BY clause.

Click on the +-button next to Sort criteria in column four to bring up this panel:

Define sort criteria

Pick Node: Post date, and click on Add.

This takes you to a panel where you get to configure the sort criterion. You probably want to see the most recent blog posts on the top, so you should pick Descending. Just leave the granularity set to Second.

Define sort criteria

Click Update.

Filter criteria

To limit the view just to blog entry nodes, you need to add a filter. Click on the +-button next to Filters in the fourth column to add a filter. This is the Views counterpart of the SQL WHERE clause – we want to select nodes where the node type is a blog entry.

The Add filters panel is very similar to the Add fields panel. You should place a tick-mark next to Node: Type. Add this filter by clicking Add.

That action will take you to the following panel:

Specify filter

We'll get around to discussing the Expose setting later in this tutorial, for now just leave it as it is (i.e. set to not expose the filter).

The setting we shall use now is the one shown in the screen shot above, and is set to restrict the view to nodes that is of the type Blog entry.

However, there is a small problem, which will be exposed if we click the Analyze button in column one: The display has no access control, but does not contain a filter for published nodes. This means that it will display unpublished blog entries along with published ones. This is not desirable behaviour, so you need to add another filter: Node: Published: Yes. Add that, click Update, and you're almost done.

noteIf you add more than one filter, the filters are combined using logical AND (see SELECT statement below). While the ability to do a logical OR would be nice, this is not currently implemented. Also note that the order of the filters are irrelevant. However, they can be re-ordered to be more readable.

Near the bottom of the view editor is a Preview-button. Now may be a good time to preview the view and make sure that it appears as we want it to.

When you click the Preview-button, a preview of the view is produced. Below the preview, there is a text box that displays the SQL query that has been created using the view editor. If you've followed the instructions in this tutorial to the letter, the query should look like this:

SELECT node.nid AS nid,
   node.created AS node_created,
   users.name AS users_name,
   users.uid AS users_uid,
   node.title AS node_title
 FROM drupal_node node 
 INNER JOIN drupal_users users ON node.uid = users.uid
 WHERE (node.type in ('blog')) AND (node.status <> 0)
   ORDER BY node_created DESC

Click Update to update the view.

Pager

Since we are restricting the number of items to display one a page to five, it is probably also a good idea to add a pager to the view. A pager is a control that let the user navigate to the next or previous page if there are more items to show than the number of items that fits on a page.

You add a pager by clicking on the link next to Use pager under Basic settings in column two. Drupal core let you choose between No pager (i.e. no way to see more items), Mini pager (a simple pager to navigate to next and previous page), and Full pager (a pager that provides more extensive navigation controls). For this tutorial, select the Mini pager. Click Update.

Save!

You're almost done specifying your first view. If you preview the view, you'll see that is now restricted to only show blog entries.

The final (and very important step) after all this is to click Save in the view editor to save the view. If you forget this final step, all your hard work constructing the view will be wasted!

When you now click the List tab, your newly created view should appear in the list of views.

List of views

You can now see how your page display of the view will appear by clicking at the path “blogs”. If there are some published blog entries on the site, you should see those listed with the fields you've selected to be displayed. On my site, it looks like this:

List of recent blog entries

How it looks depends of course on the blog entries that exist on the site.

Creating a view with a block display

A page display is a display of a view that is intended to be displayed as a page. It has a path, and that path can be used in an URL to link to this page, for example by adding that path to a menu as described in the previous section. A lot of content in Drupal is exposed as pages.

Drupal, however, as another method for exposing content, known as a block display. A block display appears as part of a page, not as a page in itself.

The steps to create a view with a block display are almost identical to the steps necessary to create a view with a page display.

So to create a block display, you can go back and replay all the steps in the previous section with two small changes: When you add a display, you should select Block display instead of Page from the drop-down menu. And you should skip the step where you set up the path (block displays don't have a path).

However, this is quite unnecessary since the Views module lets you add more than one display to a view. Instead of creating a new view with a block display from scratch, you can add a block display to the “blogpages” view you've just created.

To do this, locate the “blogpages” view in the view list and enter the view editor by clicking on Edit.

Then, in the drop-down menu in column one select Block and then click on Add display. This adds a block display to the view. After you click Save, the “blogpages” view will have a Page display and Block display, in addition to the Default display.

The default display is not very pretty and not very easy to read, so we want to override certain settings in it for a nicer block display.

The first setting we want to change is the Style, which you'll find under Basic settings in column two. The default display uses the Unformatted style.

Make sure you have the block display selected, and click on the link to change Style. In the style panel, first click on the Override-button to indicate that you want to override the default. Then select HTML List-style. This is how this panel should be set up:

Overriding the default style

Click Update. This should take you to another panel to set overriding style options.

Leave Grouping field as <None>, and select Unordered list as show below:

Set style options

Click Update again, to finalise the style settings.

Next, turn to the settings for Fields in column three, and visit each field in turn. Make sure that the block display is still selected, and override the defaults and remove the text in the Label field. We want a compact block display, and the labels take up to much space. Remember to click Update after each change.

Now, navigate to the Row style settings in column two, and click on the o-button to get to the panel where you can change its settings. You need to override its settings too. In the block display, you want to show all three fields inline. To make this happen, place a tick-mark to the left of each field in the Inline fields-section. Then enter a hyphen as Separator. The panel should look like this:

Set style options

Click Update.

As our final override, we are going to remove the pager from the block view. Click on the link to the right of Use pager under Basic settings, and select No. Click Update again.

That defines the block display of the view. Remember that you must click Save to save all your work in the view editor.

Set style options

The screen shot above shows the screen editor panel after the block display has been added. Notice that the settings inherited from the default display (e.g Title) are in grey, while those overridden (e.g. Style) are in black.

As already mentioned, block displays don't have a path. Instead, they are added to the site through the administrator's GUI interface: Administer » Site building » Blocks. When you navigate there, you will find the block display of this view listed in the section with the heading Disabled as “blogpages: Blog”. You enable it by dragging it and dropping it into the section of a page where you want it to appear.

Once you click Save blocks, the page will reload with the block displayed in the location you've chosen.

Block display

The screen shot above shows the block display with the five most recent blog entries may look like when placed in the left or right sidebar.

Creating a view with an RSS feed display

The Views module can also create an RSS feed for any list. This is done by adding an RSS feed display.

So once again, go back to the view list and click on Edit in the “blogpages” row to enter the view editor.

Then, in the drop-down menu in column one, select Feed and then click Add display. This adds an RSS feed display to the view.

Next, you need to change the row style to be compatible with the Feed display. The Fields row style that we have used until is not supported for RSS feeds. Click on the link next to Fields under Basic settings in column two, and set this to Node. and click Update. This leads to a panel to set Row style options. Select Use default RSS settings, and click Update again.

You also need to supply a path so that the RSS feed can be accessed through the URL. If you want to, we can use arguments in the path, and they will work just like they do with the Page display.

We've defined the “blogpages” view to use one argument, the user name of the creator. We use the same argument for the RSS display of the same view:

Path for RSS feed

After you click Save, the “blogpages” view will have a Feed display added. The screen shot below show a page that links to this RSS feed:

RSS feed

Creating a search function

The Views module provides a very powerful mechanism for custom searches through something called exposed filters.

As noticed, the panel for configuring filters let the designer expose a filter. Exposed filters are filters that users can interact with by supplying an argument to the filter. To create an exposed filter, you first have to create a normal filter, then click Expose.

One common use of exposed filters are to set up searches to let users search for terms that appear in taxonomies (Taxonomy: Term) or in both content and taxonomies (Search: Search Terms). In this tutorial, we'll do the latter.

To have a search function, you must enable the Search module that is an optional part of the Drupal core. So if you have not done so already, navigate to Administer » Site building » Modules and tick the Search box within the Core - optional-panel. Then scroll down to the bottom of the page and click Save configuration before going back to the view list and click on Edit in the “blogpages” row to enter the view editor.

Next, add a filter by clicking on the +-button next to Filters in the fourth column. The filter to add is Search: Search Term.

Specify filter to search for terms

Click Add. As usual, this brings up a panel to configure the filter.

Click Expose, and change the label to “Search for:”. Leave the other settings as they are.

Specify filter to search for terms

Click Update to save the exposed filter.

Just for good measure, you should also change the Style setting of the page display to Table for a nicer presentation format. Click Save to save the view.

The screen shot below shows the view as it appears when the Table style is used for layout of the page display. The search box is the exposed filter. You search by typing in the term you want to search for, and click Apply.

Specify filter to search for terms

The Views module makes it very easy to create both free text searches and taxonomy searches for a Drupal-based website.

noteDrupal does not compute the indexes for searches in real-time because that would slow the entire website down. Maintaining such indexes is instead a periodic task run by cron. Don't expect content to be searchable immediately after it has been created.

Working with taxonomies

How to do these in D7:

taxonomy/term/1,2
taxonomy/term/1+2
taxonomy/term/1/2
taxonomy/term/1/all

The right solution to is to add a relationship: Missing Taxonomy Term Description on Taxonomy Page with Enabled Taxonomy Term View.

Final word

Delete stuff as it is merged ito D7.


Last update: 2016-12-26 [gh].