Listing content

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.

Getting ready

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.

How to do it…

Let's list the Views listing content:

  1. Visit Structure and then Views. This will bring you to the administrative overview of all the views that have been created:
    How to do it…
  2. Click on Add new view to begin creating a new view.
  3. The first step is to provide the View name of Articles, which will serve as the administrative and (by default) displayed title.
  4. Next, we modify the VIEW SETTINGS. We want to display Content of the type Articles and leave the tagged with empty. This will force the view to only show content of the article content type.
  5. Choose to Create a page. The Page title and Path will be auto populated based on the view name and can be modified as desired. For now, leave the display and other settings at their default values.
    How to do it…
  6. Click on Save and edit to continue modifying your new view.
  7. In the middle column, under the Page settings section we will change the Menu item settings. Click on No menu to change the default.
  8. Select Normal menu entry. Provide a menu link title and optional description. Keep the Parent set to <Main Navigation>.
    How to do it…
  9. Click on Apply at the bottom of the form.
  10. Once the view is saved you will now see the link in your Drupal site's main menu.

How it works…

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.

Note

Nodes are labeled as Content in Views and you will find throughout Drupal this interchanged terminology.

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.

There's more…

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 in Drupal Core Initiative

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).

Views and displays

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:

  • attachment: This is a display that becomes attached to another display in the same view
  • block: This allows you to place the view as a block
  • embed: The display is meant to be embedded programmatically
  • Entity Reference: This allows Views to provide results for an entity reference field
  • feed: This display returns an XML based feed and can be attached to another display to render a feed icon
  • page: This allows you to display the view from a specific route

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.

Format style plugins: style and row

Within Views there are two types of style plugins that represent how your data is displayed – style and row.

  • The style plugin represents the overall format
  • The row plugin represents each result row's format

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.

Using the Embed display

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.

See also

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset