Views: Saved Lists of Search Results

As described earlier in the 'Drupal Fundamentals' Guide, a view is a set of search results. A view may be displayed as a page or as a block within a page (more on that in the 'Blocks' guide). The search criteria are saved within a view configuration, and when the view is displayed, it shows a list of whatever nodes currently meet those criteria. To add, edit, or view the views within a site, click on the 'Views' link in the administration menu.

After clicking the 'Views' link, a user will see table containing the list of views available. (See screenshot below.) Some views Drupal provides by default - such as 'Recent Posts.' Most views are custom. The views list shows the name of the view, the title of the view (if it has one), a description (only used in administration), the kind of view it is (either full page or block), and if it is a page, the URL to the page.

A bit of view terminology:

  • Name - the unique name used to call the view within other nodes. Also determines the CSS elements in the design for that view. This means if you change the name of a view, you will lose any theming applied to it!
  • Fields - the fields that will display in the view if the view uses fields. (Fields are not used in full node or teaser views.)
  • View Type Full Node - displays the entire node for any nodes that match the search criteria
  • View Type Teaser List - displays a linked title and a teaser blurb about each node that matches the search criteria
  • View Type Table - displays the fields chosen in 'Fields' in a table - each field in a column
  • View Type List - displays the fields chosen in 'Fields' in an unordered list
  • View Type Grid - display the fields chosen in a 4 column across grid (perfect for image gallery views)
  • Filters - the search criteria (e.g. only published nodes, only image nodes, etc.)
  • Exposed Filters - the search fields that are exposed to site visitors viewing the view. A field must be part of a filter before it can become an exposed filter
  • Arguments - additional search criteria that are passed into the view when the view is called. This allows the view to change based on its context. (e.g. find related nodes to the currently viewed node)
  • Sort Criteria - how the search results are ordered

To edit a view, click on the 'Edit' link to the right of that view. The resulting page will look something like the one to the right. It's a good idea to name views in a standardized way. One option is to name the view based on the content type it returns. (e.g. 'event_index' for calendar - an index of event nodes) Again, once a view has been themed, do not change the name.

Access to a view may be restricted based on role - a higher level of control than a generic node.

After naming the view something unique, you need to decide if the view will appear as a page (e.g. a calendar page) or as a block (e.g. upcoming events block on the home page). If you create it as a page, you will need to provide a URL. Again, it's a good idea to keep this naming standardized. (Especially if you plan on using the View Page content type. More about View Pages.)

Next, choose the view type. In the list above, there is a description of the most common view types and their uses. Usually views are either table or list views.

The Title of a view is optional. If you plan on embedding the view into a View Page, it's best to leave this blank and set the title in the View Page node.

You can limit the number of results that appear in a view. For a page, you can set 'nodes per page'. If you do not enable a pager, the view will only display that number of results. For a block, you can set 'nodes per page' (no paging). This is particularly useful if, for example, you only want to display two events in an upcoming events block on the home page.

There are also places for content within a view - the Header, Footer, and Empty Text. The Header is displayed above the search results. The Footer is displayed below the search results, and the Empty Text is displayed where the search results would be if there are no search results. A couple of interesting idiosyncrasies here. If there are no search results and the Empty Text is displayed, the Header and Footer will not display. Thus, the Header and Footer only display if the view returns results. If you plan on calling the view from a View Page, it is better to leave the Header and Footer blank, and place the description text within the body of the View Page.

As mentioned earlier, the filters are the search criteria. To add filters, expand the filters section of a view, select a filter from the drop-down, and click the 'Add Filter' button to the right of the selection box.

A good practice is to always restrict views to published nodes, so the first filter to include is Node: Published Equals Yes.

Most views are only of one content type - a directory only includes people and a calendar only includes events. So, the next thing to add as a filter is Node: Type Is One Of and select the type or types that you wish to include.

If you wish to allow site visitors to seach on views, you can 'Expose' the filters.