Conditionally Display View Fields using Views Conditional in Drupal

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.

Getting Started

This module requires no additional libraries and can simply be installed with Composer.

Using Composer:

composer require drupal/views_conditional

Go to Extend and install the module.

Views Conditional 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).

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.

Figure 2

Creating the View

We will create a Table View with a page that lists these Articles as shown in Figure 3.

Figure 3

The result of this View looks like this:

Figure 4

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:

Figure 5

Now, let’s walk through the steps of how to achieve this.

Step 1 – Add the “promoted to front page” field to the View.

Figure 6

Step 2 – Then add the Views Conditional field to the View:

Figure 7

Then configure the field as shown in Figure 8. We have highlighted the important areas.

Views Conditional - field
Figure 8

Save the View.

Step 3 – Set the Title field to be excluded from the display:

Figure 9

Step 4 – Set the Promoted to front page field to be excluded from the display:

Figure 10

At this point here is our View configuration:

Figure 11

And here is our current View results:

Figure 12

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:

Figure 13

And here is what the Promoted to front page is providing:

Figure 14

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:

Figure 15

Save the View and now our View results will look like this:

Figure 16

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:

Figure 17

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.

Summary

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.

2 thoughts on “Conditionally Display View Fields using Views Conditional in Drupal”

  1. how is this module better than creating a global custom text and working with twig?
    is it faster? or is it just more comfortable?

Leave a Comment

You have to agree to the comment policy.

This site uses Akismet to reduce spam. Learn how your comment data is processed.