Upgrade Your Drupal Skills

We trained 1,000+ Drupal Developers over the last decade.

See Advanced Courses NAH, I know Enough

A simple tour on using Drupal 8 Views contextual filters

Parent Feed: 

Views added to Drupal core is one of the most frequently mentioned Drupal 8 benefits. Drupal Views gives us a UI for creating data collections based on any desired criteria. One of the ways to fine-tune the results is to use Drupal Views filters. A level higher from regular filters are the contextual filters that accept dynamic values. This helps us create flexible and interesting solutions. Let’s review Drupal 8 Views contextual filters in more detail.

Contextual filters & their difference from regular filters

  • Drupal 8 Views regular filters can only use one static value at a time. For example, we can filter all cars with the brand name “Renault.” We can also allow website users to choose this manually if we make the filter “exposed to visitors.”
  • Drupal 8 Views contextual filters are able to use the dynamic values that change with context. For example, each logged-in user can see “Renault,” “Volkswagen,” and/or “Nissan” cars based on the preferences they put in their user profile. So the same Drupal View will show different results to everyone at the same moment. 

Thanks to the Drupal Views UI, configuring the contextual filters requires no coding skills. They can be created and configured on the “Advanced” tab of the View. However, contextual filters demand a deep understanding of Drupal and should preferably be created by Drupal developers rather than by website administrators.

Contextual filters in Drupal 8 Views UI

An example of creating Drupal 8 Views contextual filters

We will now create a simple task tracker with Drupal 8 Views. On the “My tasks” page, each user will only see the ones assigned to them. This setup will take a few preparatory steps but they will be fun. Those who are impatient can jump directly to the “Contextual filters” part a few paragraphs below. 

1. Preparatory steps for this setup only

1.1. Creating test users

Let’s start with going to “People — Add new user” and creating test users Jack Sparrow and Frodo Baggins. 

Creating a Drupal userCreating a Drupal user (2)


1.2. Creating the “Task priority” taxonomy vocabulary

Every decent task tracker needs task priority options. Let’s go to “Structure — Taxonomy — Add vocabulary” and add these options as taxonomy terms: Blocker, Critical, and Minor.

Taxonomy in Drupal


1.3. Creating the “Task” content type

Let’s create the “Task” content type in “Structure — Content types — Add content type” and add fields to it. The “Title” field will be available by default. 

Here are the necessary fields and the field type in brackets:

  • Assigned to (user)
  • Due date (date)
  • Task priority (taxonomy — with the specified vocabulary)
  • Task image
Fields for a content type in Drupal 8

1.4. Creating tasks

Let’s create a few tasks in “Content — Add content — Tasks”, select Jack Sparrow and Frodo Baggins in “Assigned to,” and fill in all the other fields. 

2. Creating our Drupal 8 Views contextual filter

2.1. Organizing all tasks into a Drupal 8 View

And we will not organize all tasks into a View in “Structure — Views — Add new view”:

  • View Settings: “Content” of type “Task”
  • Page Settings: Create a Page
  • Page Display Settings: Table of fields
Drupal 8 Views creation

Then we save the View. As it shows fields, we need to add all Task fields to it. And here is how our unfiltered task tracker looks. We only applied a simple sorting by task priority to show “blockers” above “critical” one. But still, our Views shows tasks for all users. 

Drupal 8 Views results

2.2. Creating and configuring a Drupal 8 contextual filter

Both Jack Sparrow and Frodo Baggins would appreciate a page with their own tasks, for which we will use a contextual filter. In “Advanced — Contextual Filters — Add,” we select the “Assigned to” filter argument and save the filter.

Adding a contextual filter in Drupal 8 Views

We are immediately taken to the contextual filter configuration page. 

1) For the “When the filter is not in the URL” option, we select “Provide default value.” There, we select “User ID from logged in user” from the dropdown menu. 

Drupal 8 Views contextual filter


2) For “When the filter is in the URL or default is provided,” we select “Specify validation criteria — User ID.” In the “Action to take if filter does not validate, we specify “Display contents of “No results found.”” 

Drupal 8 Views contextual fllter (2)

2.3. Testing the Drupal 8 contextual filter

Let’s check what all users see if they visit the “my-tasks” page. We visit it in an incognito window and log in as Jack Sparrow. Success — he can only see what’s assigned to him. He will need to save Elizabeth Swann and find the Fountain of Youth.

Contextual filter by user in Drupal 8 Views (2)


Let’s now log in as Frodo Baggins. Good, he knows he needs to take the ring to Mordor and destroy it in the fire of Mount Doom.

Contextual filter by user in Drupal 8 Views

Hopefully, these guys understand their epic missions — that’s how a properly made contextual filter may save lives ;) You will always have a properly made Drupal contextual filter if you contact our Drupal team!

Get assistance with contextual filters

We have shared a simple example of using Drupal 8 Views contextual filters. Our Drupal experts ready to help you configure any kinds of contextual filters for your website and create interesting user experiences.
 

Author: 
Original Post: