Skip to main content
  • Guides & Documentation

Knowledgebase

Featured articles, how-to guides and quick tips.

If Conditionals in Twig for Multiple Fields - If, Or, Empty, Else - Drupal

When working with Drupal, you'll be theming in Twig templates.

If you need to find whether multiple fields are empty, you can use a syntax like this:

{% if (entity.field is not empty) or (entity.field is not empty) or (entity.field is not empty)  %}

{{ your markup }}

{% else %}

{{ more of your markup }}

{% endif %}

Or, if you're templating in a Views fields file (views-view-fields.html.twig), you'll have to do something a little more like this (albiet a little messier, unfortunately - stick to templating fields individually if you can, or do it via field rewriting in Drupal's Views UI):

{% if (fields.field_name|striptags|trim is not empty) or (fields.field_name|striptags|trim is not empty) %}

Don't forget to switch out the bolded text in the snippets above with your entity and field name.

If you need to check whether fields are empty, you can use something like:

{% if (entity.field_name is empty) and (entity.field_name is empty) %}

Or for a Views fields template (views-view-fields.html.twig), you'd use:

{% if (fields.field_name.content|striptags|trim is empty) and (fields.field_name.content|striptags|trim is empty) %}