A Simple Guide on Using Contextual Filters in Drupal
Do you know what is one of the most important benefits of Drupal 8 that is talked about a lot? It is the views added to Drupal core! After all, Drupal Views offer a UI for creating data collections depending on any desired criteria. If you are looking to fine-tune your results, Drupal Views filters work wonders.
A contextual filter in Drupal 8 is a higher level of regular filters that accept dynamic values which helps create interesting and flexible solutions. Let’s look at Drupal Contextual filters in detail.
We also know that the entire concept of Drupal contextual filters and relationships comes with its own set of intricacies so the best way I thought will help you understand is with an example. Go on reading if you want to get acquainted with contextual filters in Drupal 8.
What is a View Contextual Filter?
A Drupal Contextual filter gives you the facility to add or remove information to the currently displayed view. The filter acts dynamically based on the URL. There are also Drupal contextual filter multiple values. If the provided value is in the URL, the view will contain specific results. Similarly, if the value is not in the URL, the view will be filtered with other results.
How are Contextual Filters Different from Regular Filters?
When looking at Drupal contextual filters in views you can see the differences. For starters, views is a very flexible module to begin with but with contextual filters, you can enhance the use of cases for the module as per magnitude. Yes, contextual filters resemble regular filters a lot but there is one major difference — rather than setting a filter value manually, the value is taken from variables sent programmatically to the view.
While a regular filter offers all nodes from a specified user, with Drupal contextual filters and relationships for a node author, you can display all nodes written by the existing viewed user (or the same user who wrote the existing viewed node).
So while contextual filters prepare a view for filtering, the filter value is not decided on. When the view is eventually called, it is also offered with data used to complete the contextual filters.
Take for instance how contextual filter values are offered to views by the view path. If a view has the path example.com/my-view, the URL example.com/my-view/story/22 will call the view along with two values for contextual filters (in this example story and 22). However, there are more ways of offering contextual filter values as well.
All things considered, what sets standard filters apart from contextual filters may look insignificant but when are apart for everyone to see when put into practice. The best part about contextual filters is that they allow you to reuse views in a variety of situations. Contextual filters permit Views to interact with other parts of the website.
To offer you a brief:
I’m certain you need some explanations along with Drupal filter criteria so here it is:
In Drupal, we use view to get a list of information.
Picture this: We use view to get a list of all article content. You may add some filter criteria and Drupal contextual filter field also. But here all of your filter criteria are static.
Now, what will happen if you want to create a view where the filter value of the condition is not static but dynamic?
A perfect example is if you want to show all the article that is created by the currently logged-in user. Given this scenario, your normal filter criteria will not work, because the currently logged-in user will not be the same user. So, you can not fix any filter like user= “someone”.
Here you will use the contextual filter. In this type of situation is the perfect use case for the contextual filter.
An exercise to help you understand contextual filters
To understand more clearly, let's do an exercise.
- Create 2 users on your Drupal site. Let's say, they are User1 and User2. Give all users as administrators.
- Log in as User1 and create 3 content of Article type.
- Log in as User2 and create another 2 content of Article type.
- Create a view of article content.
- In the top right corner Advanced->contextual filter->add
- Select authored by and click add Apply
- So here what you have done is set the filter based on the user name of the content.
- But there is one thing: Usually the URL does not contain the user id. So what can you do in this situation since we know in contextual filters the filtering happens based on the data from URL? We will offer you our view of what can be done if it does not get the filter value from the URL.
- You can perform the following actions:
- Now place your view in a block or place where you want.
- Log in as User1 and User2. In both cases, you will see only the Article content that is created by the user. You will not see other users' articles.
Did you know?
- It is easy to extend the standard functionality of contextual filters to accpet filter ranges such as example.com/my-view/story/21--100 by installing the Views Contextual Range Filter module.
- Contextual filters were known as arguments in Views for a long time and even now, documentation and tutorials as well as Views-compatible modules continue to use the term. If you use the term argument, you should know it is either the value offered to a contextual filter or a contextual filter.
- While it is seamless to configure both an (exposed) regular filter as well as a contextual filter for the same field in your view, the results won’t always be what you expect.
So there you have it! To get assistance on contextual filters, the Drupal 8 views contextual filter query parameters, Drupal 8 views contextual filter multiple values, and more, get in touch with our Drupal development experts today! We help configure any types of contextual filters for your website so that you gain one-of-a-kind user experiences