Share icon
Conditional views

Hi, We are here today with new interesting tips and tricks about drupal 8 views.

In Drupal 7 we now that if we want any conditional based result we can use "Views PHP" to apply and generate the conditional based result, But How in drupal 8 we use that conditional views results, this tutorials explain it to you.

Let take a example:

Suppose you want to show additional text with Gender field

For that I have created a field List(text) field with 3 option: Male,Female and other


Now I create 2 or 3 account with different selection like first account to male, other to female and so on

and create the user view to display on any page, see example screenshot

view screeshot

I added 2 field and 1 global custom text for our custom conditional based output

In global custom text code is like below to display result like 

<div class="intro">
{% if  field_gender == "male" %}
He is {{ name }} and gender is {{field_gender}}
{% elseif field_gender == "female" %}
she is {{ name }} and gender is {{field_gender}}
{% else %}
He is {{ name }} and gender is {{field_gender}}
{% endif %}

And about result will below screenshot


user view output

Hope you understand the above process, by using this you can display conditional output with twig in drupal 8 views


Add new comment

Restricted HTML