Skip to content
Tutorials Center
My learning

Topics

Lesson 15 of 37 ยท Views and Blocks

Views Basics

Build your first Drupal View: choose what to show, add fields, filters and sorts, set a page or block display, format as a table or grid and paginate results.

Views is Drupal's query builder with a graphical interface. It lets you say "show the ten most recent published events, newest first, as a grid with a photo and date" without writing SQL or PHP. Views ships in core, powers the admin content list, the taxonomy term pages and most listings on real sites, and is probably the single feature that makes Drupal site building fast.

You should already know content types and fields from Content Types and Fields. Views reads those fields, so a clean model makes Views easy.

The anatomy of a view

A View is a stored (configuration) description with these parts:

  • Show: the base table: Content, Users, Taxonomy terms, Media, Files, Comments... plus an optional type filter.
  • Format: how the list looks (unformatted list, table, grid, HTML list) and whether each row shows fields or a rendered content in a view mode.
  • Fields: which values to output.
  • Filter criteria: which items qualify.
  • Sort criteria: the order.
  • Pager: how many items per page, with or without a full pager.
  • Displays: a View can have several, such as a Page (has a URL and menu entry), a Block (placed in a region) and a Feed (RSS).
flowchart LR
    A["Base: Content of type Event"] --> B["Filter: published = yes, date >= today"]
    B --> C["Sort: date ascending"]
    C --> D["Fields: title, date, place"]
    D --> E["Format: table"]
    E --> F["Displays: Page /events, Block"]

A View is a pipeline: source, filter, sort, format, display.

Build one step by step

  1. Go to Structure > Views > Add view (/admin/structure/views/add).
  2. View name: "Upcoming events". Machine name upcoming_events.
  3. Show Content of type Event, sorted by Newest first.
  4. Tick Create a page: page title, path events, display format Unformatted list of fields, items per page 10, and use a pager.
  5. Optionally Create a block.
  6. Click Save and edit.

You land in the Views UI, which has a live preview at the bottom. Whenever you change something, look at the preview.

Fields

Under Fields, click Add and tick the fields you need (title, date, place). Each field has settings: formatter (like the display formatters you met in Field Types, Widgets and Formatters), label, "link to content", rewrite results with tokens, and "hide if empty". You can rewrite output with replacement patterns such as {{ title }}.

Filter criteria

Add filters to restrict rows. Always include Published (= Yes) so unpublished content does not leak. Common additions:

  • Content type (= Event).
  • Date field with an operator, such as "greater than or equal to" a relative date today.
  • Title (contains).
  • Taxonomy term (is one of).

Filters can be grouped with AND and OR logic.

Sort criteria

Add one or more sorts: Event date asc, then Title asc as a tie breaker. Remove default sorts you do not want, because the order of sorts matters.

Formats

Pick a Format for the display:

  • Unformatted list: one row after another, easiest to style.
  • Table: sortable columns and column controls.
  • Grid: responsive columns (the Grid format in core).
  • HTML list: <ul> or <ol>.

Row style is either Fields (you pick) or Content (each row rendered with a view mode such as Teaser). Content with a Teaser view mode is the most consistent choice for lists that should match the site's design.

Pagination and performance

A pager limits rows, so the database does not return thousands of items. Options: Display a specified number of items, Paged output, full pager, Mini pager. Under Advanced > Caching you can enable Tag based caching so the list refreshes when content changes. A fast list is a cached list, a topic we revisit in the Performance chapter.

Configuration of a view

A view is configuration, so it exports to views.view.upcoming_events.yml. A fragment shows the sort and filter definitions:

YAML
id: upcoming_events
label: 'Upcoming events'
base_table: node_field_data
display:
  default:
    display_options:
      pager:
        type: mini
        options:
          items_per_page: 10
      filters:
        status:
          id: status
          value: '1'
        type:
          id: type
          value:
            event: event
      sorts:
        field_event_date_value:
          order: ASC

Because it is text, a View can be reviewed, deployed and rolled back like code.

A view engine in miniature

The stand-in below models the essential pipeline: take rows, apply filters, sort, limit and show fields. It shows why the same data can feed many listings. It is not Drupal's actual Views engine, which builds SQL queries.

JS Stand-in: a view as a pipeline

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Notice that the unpublished event never appears: the status filter is why you should always add it.

Two displays, one view

A single View can feed a page and a block with shared settings. The block might show only three items, while the page shows ten with a pager. This stand-in overrides one setting for a second display, as Views does with "This page (override)".

JS Stand-in: displays share and override settings

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Good habits

  • Name views clearly and add a description.
  • Always filter by published status.
  • Prefer Content row style with view modes for consistency.
  • Limit items and enable caching.
  • Export configuration after editing.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What are the two most common displays of a View?

    Page displays have a URL; Block displays can be placed in a region.
  2. 2. Which filter should almost every content listing include?

    Without it, unpublished content could appear in lists.
  3. 3. What is stored when you save a View?

    Views are configuration entities, exportable and deployable.
  4. 4. What does the row style 'Content' do?

    Content row style reuses the display configuration of a view mode such as Teaser.

Summary

  • Views builds listings from entity data with filters, sorts, fields, formats and pagers.
  • One View can have several displays (page, block, feed) that share and override settings.
  • Always filter by published status, limit rows and use caching.
  • Views are configuration, so they export to YAML.

Next: Views Contextual Filters, Relationships and Exposed Filters