Views does one thing, and it does it well: listing content. The power behind the Views module is the amount of configurable power it gives the end user to display content in various forms.
This recipe will cover creating a content listing and linking it in the main menu. We will use the Article content type provided by the standard installation and make an articles landing page.
The Views UI module must be enabled in order to manipulate Views from the user interface. By default this is enabled with the standard installation profile.
Let's list the Views listing content:
The first step for creating a view involves selecting the type of data you will be displaying. This is referred to as the base table, which can be any type of entity or data specifically exposed to Views.
When creating a Views page we are adding a menu path that can be accessed. It tells Drupal to invoke Views to render the page, which will load the view you create and render it.
There are display style
and row
plugins that format the data to be rendered. Our recipe used the unformatted list style to wrap each row in a simple div
element. We could have changed this to a table for a formatted list. The row display controls how each row is output.
Views has been one of the must-use modules since it first debuted, to the point that almost every Drupal 7 site used the module. In the following section we will dive further into Views.
Views has been a contributed module up until Drupal 8. In fact, it was one of the most used modules. Although the module is now part of Drupal core it still has many improvements that are needed and are being committed.
Some of these changes will be seen through minor Drupal releases, such as 8.1x and 8.2.x, as development progresses and probably not through patch releases (8.0.10).
When working with Views, you will see some different terminology. One of the key items to grasp is what a display is. A view can contain multiple displays. Each display is of a certain type. Views comes with the following display types:
Each display can have its own configuration, too. However, each display will share the same base table (content, files, etc.). This allows you to take the same data and represent it in different ways.
Within Views there are two types of style plugins that represent how your data is displayed – style and row.
For example, the grid
style will output multiple div
elements with specified classes to create a responsive grid
. At the same time, the table
style creates a tabular output with labels used as table headings.
Row plugins define how to render the row. The default content will render the entity as defined by its selected display mode. If you choose Fields you manually choose which fields to include in your view.
Each format style plugin has a corresponding Twig
file that the theme layer uses. You can define new plugins in custom modules or use contributed modules to access different options.
Each of the available display types has a method to expose itself through the user interface, except for Embed. Often, contributed and custom modules use Views to render displays instead of manually writing queries and rendering the output. Drupal 8 provides a special display type to simplify this.
If we were to add an Embed display to the view created in the recipe, we could pass the following render array to output our view programmatically.
$view_render = [ '#type' => 'view', '#name' => 'articles', '#display_id' => 'embed_1', ];
When rendered, the #type
key tells Drupal this is a view element. We then point it to our new display embed_1
. In actuality, the Embed display type has no special functionality, in fact it is a simplistic display plugin. The benefit is that it does not have additional operations conducted for the sake of performance.