Views
There is a lot of functionality in views. It is one of the most important and versatile modules in Drupal. Read the Views documentation at drupal.org updated May 2023.
This guide will just cover the bare functionality.
Overview
The core Views module allows you to fetch content from the database of your site and present it to the user as lists, posts, galleries, tables, maps, graphs, menu items, blocks, reports, forum posts and much more. Different content types including nodes
, users
, and other entities can be displayed.
The Views UI
, a submodule within Views, provides a graphical interface which can access the powerful SQL query builder to access virtually any information in your database and display it in almost any format.
Different displays
can present the query results as pages with fixed URLs on your site (or URLs accepting arguments), blocks, feeds, or panel panes.
Existing Views
If you enable the Views UI
module you can see all views in your system by going to Structure
-> Views
.
You can edit or delete a view by clicking the respective link for the view you wish to manipulate.
By editing a view, you can see the various components of a view:
Adding a New View
You can add a new view by going into Structure
-> Views
-> Add
.
Displays
Each view can have multiple displays which are usually used to display the same information in slightly different ways. Don't create too many though:
You can add additional displays clicking the
Add
button.You can also use the views display menu for more options:
A display's title can be set here:
Additionally, each display may have settings specific to the display type:
Page Display:
Block Display:
Feeds Display Settings:
Format
Depending on the format of your view, your view output can vary widely. Some include the ability to output specific fields, and others include options to output entities themselves. You will find the format here:
By clicking the current format name you can change the format being used:
If you select a format such as
Table
, you can select from a large list of all available fields to be added to the display. (Note: You can add a field to just the particular display you are on, or all displays):You can view the list of fields added to the display here:
Filtering and Sorting
You can use certain fields and other criteria for filtering and sorting your results. Note: You can "expose" filters and sorting criteria in a view, to allow users to specify these values dynamically.
By clicking the
Add
button you can choose from a large list of all available fields, filters and sorting criteria. Note: You can add a field to just the particular display you are on, or all displays.
Headers/Footers/No Results
You can specify specific content for the views header, footer and also define the behavior when a view returns no results:
Pager
You can set pagination settings for your view results. There are options for a full pager, mini pager, or no pager at all. Also this is where you can specify the number of items per page as well as the offset. i.e. skip the first result. (This is more useful than you might think):
- You can set the pager options:
- You can set the pager options:
Language
- If your site is configured for Multilingual support, you can specify language-specific settings for your results.
Advanced
The advanced section provides functionality useful in more advanced use cases.
Contextual Filters
From drupal.org - Contextual filters:
Views is a highly flexible module to start with, but the contextual filters increase the use cases for the module by an order of magnitude. Contextual filters work similarly to regular filters, but there is one important difference. Instead of setting a filter value manually, the value is fetched from variables sent programmatically to the view. For example, if the view shows a list of nodes, you could have a contextual filter for the node author which would limit the list to nodes written by a specific user. The url might be /author-nodes/52
where 52 is the user id.
A regular filter could give you all nodes written by a specified user. A contextual filter for a node author would be able to display all nodes written by the currently viewed user, or the same user who wrote the currently viewed node. The concept is that contextual filters prepare a view for filtering, but the filter value is not yet determined. When the view is eventually called, it is also provided with data used to complete the contextual filters.
For a tutorial showing how to use contextual filters, check out How to use Contextual filters in Drupal 8 from ostraining.com - Oct 2019 and A Simple Guide on Using Contextual Filters in Drupal - Sep 2022.
Relationships
You can use Relationships to make connections from your view results to other entities. This is commonly done through entity reference fields. By making these connections you expose additional fields and information to views.
Exposed Form
Advanced settings for a view's exposed filters. This allows you to display the exposed filters in a block so you can put them in a sidebar or other region of your site.
Other
Machine name
- The machine name for this specific displayAdministrative comment
- Leave comments for admins related to this particular view or displayUse AJAX
- Turns on AJAX processing for filters and pagination so the view page updates without a full page reloadHide attachments in summary
- Hide attachments when displaying a contextual filter summaryContextual links
- Whether not to show contextual menu for this view (This is the gear icon that appears when you hover over a view).Use aggregation
- Aggregate results togetherQuery settings
- Advanced database optionsCaching
- View cache settingsCSS class
- Add CSS classes to your view
Resources
Questions
What are Views in Drupal, and what functionalities do they offer?
Allows you to fetch content from the database of your site and present it to the user as lists, posts, galleries, tables, maps, graphs, menu items, blocks, reports, forum posts and much more.
How can you add a new view, access existing list of all views and enable? and what are `displays` in Drupal Views?
To add a new views by navigate to Structure > Views > Add
. To enabling the Views UI
module and navigating to Structure > Views
. Displays are different presentations of the same view, such as pages, blocks, or feeds, allowing the same data to be shown in various formats.
What are some of the format options available for displaying views, and how can you add fields to a view display?
Formats include tables, lists, grids, and more, each determining how the view's output is presented. To add a field click Add button within the Fields section this opens a modal with a list of available field, browse through the list of fields displayed and then you can filter the list by typing a keyword in the search box at the top once you find the desired field, click its checkbox to select it.
What is the function of exposed filters, pager and Contextual Filters?
Exposed filters allow users to dynamically specify filtering criteria for the view's results. Pagination settings can be configured within the view to control how many items are displayed per page and how users navigate through the results. There are options for a full pager, mini pager, or no pager at all, you can specify the number of items per page as well as the offset. Contextual filters in Drupal allow you to dynamically filter content in a view based on information from the URL, user context, or other dynamic inputs, filters content based on URL arguments, current user roles, example show articles authored by the currently logged-in user or other contexts.