2

I am writing a template and if 2 fields, one of which is required the other is not. I would like to display the one thats not required if has been filled out by the user.

I have tried numerous things

{% if content.not_required %}
 {{ content.not_required }}
{%else%}
  {{ content.required }}

{% if content.not_required  is null %}
 {{ content.required }}  
{%else%}
  {{ content.required }}  

{% if content.not_required  == false %}
 {{ content.required }}
{%else%}
  {{ content.required }}

{% if content.not_required  == null %}
 {{ content.required }}
{%else%}
  {{ content.required }}

{% if not  content.not_required %}
 {{ content.required }}
{%else%}
  {{ content.required }}

all of these display all stories the same whether or not content.not_required was filled in.

Is there a way to do this in twig or do I need to set up some variable in a preprocess function?

3 Answers 3

4

There is an issue about this on Drupal.org: Checking for empty fields in Twig templates is complicated.

Use the following syntax and keep an eye on updates:

{% if content.not_required|render %}
    {{ content.not_required }}
{% else %}
    {{ content.required }}
{% endif %}
1

First, you are not referencing the field name right. All fields from Drupal are prefixed as field_. So it should be field_not_required if you created the field.

You also don't need a module, you don't need to check the render array. You can just do {% if node.field_name is not empty %} which is standard Twig. This also works for any entity that passes its data object into the template, like Paragraphs or Media.

Here is an example from an actual project:

  <div class="hero__content">
    {% if node.field_hero_title is not empty %}
      {{ content.field_hero_title }}
    {% else %}
      <h2 class="hero__title">{{ node.label }}</h2>
    {% endif %}

    {% if node.field_hero_subtitle is not empty %}
      {{ content.field_hero_subtitle }}
    {% endif %}

    {% if node.bundle == 'article' %}
      <h5 class="hero__description">{{ node.getCreatedTime|format_date('article_full_date') }}</h5>
    {% endif %}

    {% if node.field_hero_description is not empty %}
      {% autoescape %}
        {{ content.field_hero_description }}
      {% endautoescape %}
    {% endif %}
  </div>

This checks to see that the field is not empty before rendering the render array in the template.

If the field you are using is a boolean:

{% if node.field_not_required.value == 1 %}
0

The solution is to install this module: twig_field_value

With this module you can drill down to the effective content of a field, without the wrappers or standard field-labels (html-wrappers or labels can give the false impresion of a field not being empty in a -n "if statement"). By adding this code"|field_value", you can get only the bare value only of a field:

{{ content.field_name|field_value }}

Now you use a simple Twig if statement to see if a field is really not empty:

{% if content.field_name|field_value %}
    <p>Field "field_name" is not empty, the value is {{ content.field_name|field_value }}</p>
{% endif %}
1
  • You don't necessarily need a module, you don't need to check the render array. You can just do {% if node.field_name is not empty %} which is standard Twig. This also works for any entity that passes its data object into the template, like Paragraphs or Media.
    – Kevin
    Commented Oct 2, 2017 at 15:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.