Loading...

Drupal 8 Views Module

Can You Use Drupal 8 Views Module on the Backend?

Many would agree that the Views module is one of the most robust modules in Drupal. In fact, it is so powerful, as well as popular, that the once D6/D7 contributed module is now a core module in Drupal 8. Therefore, it makes sense by now that many of us have leveraged this module for many things on many sites.

We often see it used on the front-end to power page listings whether it be an archive with various filters or an image carousel. But can you harness the power of Views on the backend?

Well, guess what? As you may have already noticed, Drupal 8 already does this from the point a fresh install has been created! Ever took a look at the "Content", "Custom block library", or "People" views? Those views all display entity listings in the administration area of a Drupal site.

Why Use Views on the Backend? 

After all, you can build a page listing for an entity type using the class \Drupal\Core\Config\EntityConfigEntityListBuilder. Although that will allow you to create an entity listing, Views will give you the ability to not only list items, but to filter them, sort them, create relationships, change attributes based on context, and more.

Content Listing

Let's look at an example. The "Content" view is responsible for listing out all node entities. It has a single page display.

Drupal 8 Content Views Module

This listing has been formatted as a table with fields for rendering node titles, content types, authors, publishing statuses, change dates, and links for performing various admin operations. There are filters (most of which are exposed) for the publishing status, content type, title, and language of a node.

There are also other settings being used to configure things like behavior when there are no results or permissions to dictate who has access.

As you can see, views allow you one way to basically customize the user experience on the backend in a simpler way using the UI!

Create an Admin View of Your Own

What if you want to create your own? You could do this as you're building a website by simply mimicking the setup of the admin views already installed. What if you have a view tied to a specific module you created? You would want it to automatically install when the module is installed. You would also want it to be removed when the module is removed. You would not want to manually do this, particularly when your module is contributed.

The Media Entity module implements a solution to this. Using Drupal 8's configuration management, the file /media_entity/config/install/views.view.media.yml is automatically imported into your sites existing configuration once the module is installed and enabled. This creates the Media view which adds a page listing, a filterable list of media entities.

You can do something similar by creating the view as you would any other, then export the configuration of the view into a views.view.VIEW NAME.yml file using the UI or drush config-export. You can then move that file to the /config/install directory within your module. Once your module is installed, your view will be available for use.

This is yet another powerful way to use views. Why not try using it to further customize the user experience on the backend of a site?