The Views Conditional module is helpful if you want to display custom text depending on other field values in the View. It can be used to conditionally hide field values based on other View fields.
The module allows you to add a custom “conditional field” to the View, just like any other View field. Then you can use this field to customize the output based on the values of other fields. If this sounds confusing, it will become much clearer as we walk through a real example in this tutorial.
A common use case is if you have a View table and you want to change the content of a column depending on the value of another View field. We will demonstrate this now.
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
This module requires no additional libraries and can simply be installed with Composer.
composer require drupal/views_conditional
Go to Extend and install the module.
Drupal Set Up
For this demo, we have installed a standard Drupal site and created 2 nodes of type Article and that’s it (See Figure 1).
As we will be using the “Promoted to front page” as our condition in this example, we have ensured that only Article 2 is promoted to the front page as shown in Figure 2.
Creating the View
We will create a Table View with a page that lists these Articles as shown in Figure 3.
The result of this View looks like this:
If you want to learn more about Views then check out our “Getting Started with Views in Drupal” tutorial (text and video).
Add our Views Conditional Field
Here is where we will detail the benefits of using this module.
As we have promoted only Article 2 to the front page, here is the end result of what we are trying to achieve:
Now, let’s walk through the steps of how to achieve this.
Step 1 – Add the “promoted to front page” field to the View.
Step 2 – Then add the Views Conditional field to the View:
Then configure the field as shown in Figure 8. We have highlighted the important areas.
Save the View.
Step 3 – Set the Title field to be excluded from the display:
Step 4 – Set the Promoted to front page field to be excluded from the display:
At this point here is our View configuration:
And here is our current View results:
You will notice that our Views conditional field is not working as yet. That is, the text “promoted to front page” is not being appended next to Article 2.
We have set the Views conditional field to use binary values for comparison but the Promoted to front page field is providing “on/off” values, so there is no match.
Here is what the View conditional field is using to compare:
And here is what the Promoted to front page is providing:
As you can see, “on/off” will never be equal to “1/0”.
Step 5 – Update the Promoted to front page View field to provide binary values as shown in Figure 15:
Save the View and now our View results will look like this:
Our Views Conditional field correctly appends the text “promoted to front page” after the Views title.
If we then promoted Article 1 to the front page, the View results will be updated accordingly:
Tip – It is important to note that if you want to use other View field replacement variables as we have done inside the Views conditional field, then the Views conditional field should be last (or at least after the field you want to use) as shown in Figure 11. The order of the fields will determine what is available to the Views Conditional field. Once the Views Conditional field is last, all the fields above will be available as replacement variables.
We have demonstrated to can use the Views Conditional module to customize output based on other View fields. This module provides a special View field that we can then configure to provide customized output (using replacement variables from other View fields) based on numerous configurable conditions.
We have touched on just one of the configurable conditions that this module provides, but there are many other available conditions listed on the module’s page.