Don’t forget to subscribe to our YouTube channel to stay up-to-date.
In Drupal, content is stored in the database. Views is a query builder that allows the user to extract content from the database and output it in various displays such as tables and lists. With Views, the user does not have to know or write any SQL queries. If you want to create a page or block in Drupal that lists any kind of content based on different filter criteria, you should use Views!
In this tutorial, we will explain what Drupal Views are and how to create it. We will also demonstrate some simple customizations such as page settings, filters, sorting, display options, exposed filters, permissions, creating a View Block and creating an admin page using Views. By the end of this tutorial, you will know how to create and customize a Drupal View.
Drupal Views Series
1. Getting Started with Views in Drupal
2. Create Infinite Scroll pages using Views Infinite Scroll in Drupal
3. Hide Block if no Results are Returned using Views in Drupal
4. Bulk Update Content using Views Bulk Operations in Drupal
5. Add Custom Tab to User Profile Page with Views in Drupal
6. Conditionally Display View Fields using Views Conditional in Drupal
As of Drupal 8, Views already comes packaged with Drupal core so there is no need to download anything. If you installed Drupal using the standard installation profile then it’ll be already installed.
Go to Extend and make sure Views and Views UI are installed.
How to Create a View
In this section, we will walk you through the process of creating a very simple View that lists published Articles using the teaser view mode.
First, ensure you have enough article content on your website and ensure you are logged in as the administrator. Now let’s create the View.
1. Go to Structure -> Views (admin/structure/views) and click on “Add view”.
2. On the “Add view” page, fill out the fields as suggested in the screenshot below. All we are doing here is giving the view a name, selecting “Article” in View Settings and ticking the box “Create a page”. Every other field can be left as the default for now. Click on “Save and edit” when you are finished.
And that’s it! You have successfully created your View. If you were to go to “
/latest-articles” you can see the output of your View.
We will now explain the major parts of the Views configuration screen. We have highlighted different sections of the Views configuration screen in the screenshot below and labelled each section with a number for easier reference and explanation.
Label 1 – As the name suggests, this is the title of the View. This is the text that will appear as the title of the
Label 2 – This determines the output style of the view. Frontend themers can use this to style the output by overriding the default templates provided by Views. There are also contributed modules that can extend and provide more options such as Views Slideshow.
Label 3 – This determines how each row result of the View should be displayed. Currently it’s set to “Content” which allows us to choose different view modes like Teaser or Full Content. If you want to show just specific fields of your Articles, you can change it from “Content” to “Fields”. Then, you can select the fields you want to display in Views Config Screen Label 4.
Label 4 – Based on which Format style you choose above, this section will allow you to choose the specific fields to display.
Label 5 – This section allows you to define criteria for filtering your View results. Currently we are only showing published articles.
Label 6 – As the name suggests, we can define how to sort the View results. You can also sort on your content type fields by adding them here.
Label 7 – This is the path for your View page.
Label 8 – If you want to add your View page to any of your Drupal menus, this is where you can do it.
Label 9 – In this section you can define specific permissions for who is allowed to access your View.
Customizing your View
Let’s say we want to customize our current View to display a table layout with just Title and Body fields of the Article content type. But we also wanted to allow the user to input a text string that will filter the results against the Title. We will refer to the labelling in Figure 2 to help with our explanation.
1. Update the Format (Views Config Screen Label 2). Set it to Table. You can leave the default Table settings.
2. In doing Step 1, you may notice that Views automatically added the Title field in the Fields section (Label 4). Let’s add the Body field as well.
Click on “Add” in the Field section:
In the “Add fields” popup, search for “body” and click on “Add and configure fields”.
On the “Configure field: Content Body”, you can leave all the defaults and just click on Apply.
3. Now let’s demonstrate how to allow the user to enter text into a field that will automatically search against the Title field. In Drupal Views, this is called an “exposed filter”.
To create this exposed filter, click on “Add” in the Filter Criteria section (Label 5).
Then search for Title in the next screen and click on “Add and configure filter criteria”.
On the next screen, check the box “Expose this filter to visitors, to allow them to change it”. More options will automatically become available. Make sure to set the Operator to “Contains” and then click on “Apply”.
Tip: “Contains” means Views will use the string entered by the user and execute a SQL search in the database for any string that contains the characters entered in that order, i.e., “
...LIKE %usertext%...” in SQL.
4. Now that we have set the Table Format, Title/Body fields and exposed filters, it’s time to save the View. This step can be easy to miss so don’t forget it!
Now if you view the actual View page, the View output should look like this:
Create Block using Views
A View Block is just a regular Drupal block except it originates from Views.
For demonstration purposes, let’s say we wanted to create a block version of the View page we created above.
There are two ways to do this. You can either click on “+Add” or click on “Duplicate as Block”. Both options are shown below:
Once you have done that you will notice you will now have “Block Settings” instead of “Page Settings”. This makes sense since now we are dealing with a Drupal Block.
Go ahead and save your View. Once the View is saved, a Drupal block is automatically generated and will be listed alongside all the other Drupal blocks.
Place the block anywhere in your Drupal site as you normally would place any Drupal block into a region. Here is a quick video of how to place a Drupal block into a region. We placed ours in the left sidebar and it looks like this:
You will notice our exposed filter is not being displayed in our Block. There is one extra step involved if you want to get exposed filters to work in View blocks. On the View configuration screen, inside of Advanced, set “Use AJAX” to yes and save your View. Here is how that looks:
Now our Views Block has the exposed filter which looks like this:
Create Admin Page using Views
Let’s say we wanted to use the View page we created above but place it within the admin UI and only accessible to a person with elevated permissions (in other words, not anonymous users). This is a screenshot of the end result we are trying to achieve:
There are a few things we need to edit in our existing View to achieve this. All of the changes we have to make will be in the Page Settings of our View config screen (See Figure 2). We need to update three things:
- Label 7 – Update the Path
- Label 8 – Update the Menu
- Label 9 – Update the Access Permission
1. Updating the Path: In the Views config screen, update the Path to “
2. Update the Menu: In the Views config screen, update the Menu. Clicking on “No menu” will take you to the Menu item entry config screen. On this screen, choose Menu Tab for Type, enter a Menu link title and set the Parent to “<Administration>” and then click on Apply.
3. Update the Access: We can do this either by defining the Permission or by defining the role. For this tutorial, we will define the permission. In the Views config screen, click on “View published content” and change it to “Access the content overview page”.
4. Save your View!
Now if you go to “
admin/content/latest-articles” as a logged in user, you should see your View page. If you tried going to this same page as an anonymous user you should get Access Denied.
In this tutorial, we introduced you to Views and some of its potential capabilities by providing some simple customizations. We specifically concentrated on demonstrating how to create a View Page and a View Block which list published Articles and also included a View exposed filter which allows the user to enter a text string to search against the Title. We also touched on how you can create an admin page using Views by updating its path, menu and permissions.
Views, which is in Drupal core as of version 8, is a very powerful tool and can be highly configured and customized. We hope we helped in getting you introduced to Views and the potential it has.