How to Use Contextual Filters in Drupal 8
Contextual filters in Drupal allow you to add or remove information (content) to the currently displayed view. The filter acts dynamically based on the URL.
If the provided argument value is in the URL, the view will contain certain results, if the value is not in the URL, the view will be filtered with other results.
This whole concept implies certain complexity, so the best way to clarify it is with an example.
Keep reading if you want to grasp the idea behind contextual filters in Drupal 8!
Step #1. Create the View
For the purpose of this tutorial, I have created 11 articles with an image and body text.
The goal of this exercise is to create a block with a view displaying articles, related to the currently viewed post.
- Click Structure > Views > Add view
- Set the following options
- Show Content of type Article
- Create a Block
- Display a Grid of fields
- Choose 12 items per block
- Click Save and edit
- On the FIELDS section click Add
- Search for the Image field and select it
- Click Add and configure fields
- Link the image to the Content
Notice that I created a squared image style (Configuration > Image styles), to display it in the view. More on Image styles on a future tutorial.
- Click Apply
- In the FIELDS section click the dropdown arrow and select Rearrange
- Drag the Image field above the Title
- Click Apply
- Click Grid Settings in the FORMAT section
- Change the number of columns to 2
- Click Apply
- Click Save to save the view
Step #2. – Place the View Block
- Click Structure > Block layout
- Scroll down to the Sidebar second region
- Click Place block
NOTICE: I have also removed the 2 blocks on the Sidebar first region.
- Search for the name of your view in the search box
- Click Place block
- Restrict the block only to the Article content type
- Click Save block
- Go to the frontend of your site
- Open one of your articles.
You will see the block displaying the view the way you have just configured it. However, the currently displayed article will also appear within the view results. That is not user-friendly and looks less professional.
Step #3. – Views Contextual Filters
- Hover your mouse over the block
- Click the pencil icon and Edit view
- Click ADVANCED to display the right-hand column of Views UI
- Click Add to ad a Contextual filter
- Display only the filters related to Content
- Select the Entity (Content) ID as a contextual filter
- Click Add and configure contextual filters
This view is displayed in a block. Blocks do not have URLs to reference, so you have to add this “connection” manually.
- Click When the filter value is NOT available
- Select Provide default value
- Choose Content ID from URL
- Click Apply
- Scroll down to the Preview section
This is where you can test how the contextual filter works. The system will take the value of the Article (in this case) ID from the URL and according to that value, it will filter and present the right view.
One thing to notice is that Drupal is smart enough to internally figure out the node id, even if you have enabled Pathauto, or have changed the URL manually in the node edit form.
- Enter the ID of one of your articles
- Click Update preview
The view will display just the element related to that particular article. This is not what we want to have. As a matter of fact, it is the exact opposite of what we want to achieve.
- Click the Content: ID filter
- Scroll down and click MORE
- Check Exclude
- Click Apply
By clicking exclude, you are basically telling Drupal to display all results of the view, except the one related to the currently displayed article.
- Click Save to save the view
If you go to one of your articles, you will see a block displaying all other related publications, but not the one you are reading.
This tutorial only scratches the surface of what is possible with contextual filters. I hope this has helped you to clarify the concept a little more.
Thanks for reading!
What if you had another article about ‘wolves’? That would still show up in your view block display because you show nothing about filtering articles based on content. All this does is filter out the article that you’re currently on from that view. I feel like this article is a little bit misleading. As per your post, ‘[i]The goal of this exercise is to create a block with a view displaying articles, related to the currently viewed post.[/i]’. Well, the only reason it’s displaying articles ‘[i]related[/i]’ to the currently viewed post is because all you did was create articles that were about birds. To make your statement true, more would need to be done to relate articles to each other; whether it be through a taxonomy or other means.
Hi Anthony,
Thank you very much for your comment. That is a good point.
The tutorial tries to explain the concept of contextual filters. I could have written kitten articles and then filter out the one precise kitten article displayed in the node from the block.
If you notice (in Views UI), the contextual filters provide you with a lot more options than just using the id or the node, like the author id, the week of creation, or even a common taxonomy term. Furthermore, it’s worth to notice that you can add relationships to enhance the scope of the filter even more.
The tutorial states this : “You will see the block displaying the view the way you have just configured it. However, the currently displayed article will also appear within the view results. That is not user-friendly and looks less professional.”
The one configuring the view is the developer. The filter cannot and will not do magic.
Cheers
Very good, valid point.
I did really like his point about pathauto and url aliases..
Thanks for the hint!