13

We have a field_boolean (boolean field) on an ECK Bundle on the article content type. While doing research we came across the question "How to test for a boolean field in a twig template?" but it does not solve my problem.

What is the appropriate way to check for the boolean value?

We have attempted but they don't work:

{% if entity.field_boolean == True %}
{% if '1' in entity.field_boolean[0] %}

These appear to work, but not sure it is correct approach.

{% if entity.field_boolean[#items].value == '1' %}
{% if entity.field_boolean[#items].value %}
5
  • 1
    Did you try {% if node.field_foo.value === ... %}?
    – Kevin
    Jan 25, 2018 at 18:07
  • 1
    @Kevin Yes, but i got the following error message "Twig_Error_Syntax: Unexpected token "operator" of value "=". in Twig_ExpressionParser->parsePrimaryExpression()", which did not make sense to me. Jan 25, 2018 at 18:54
  • 1
    I'm sorry, either == or same as should work. same as is like ===. twig.symfony.com/doc/2.x/tests/sameas.html
    – Kevin
    Jan 25, 2018 at 18:57
  • 1
    @Kevin when i tried that it would always be true Jan 26, 2018 at 18:50
  • {% if entity.field_boolean[#items].value == '1' %} works for me Feb 18, 2020 at 12:25

5 Answers 5

25

Time for another promotion of my blogpost from a while ago :)

https://www.md-systems.ch/en/blog/techblog/2017/02/20/twig-and-entity-field-api-or-how-to-take-control-of-nodehtmltwig

The relevant part:

Only display a field if a checkbox is checked, optionally with custom wrapping HTML.

{% if node.field_checkbox.value %}
   <div class="only added when there are values">
  {{ content.field_name }}
  </div>
{% endif %}

You don't need == '1' or same as or anything like that, because it directly evaluates as to a boolean value. I also wouldn't recommend to use type safe checks with content entities because they are not type safe. Values are stored in the database and anything coming out from there is a string but sometimes values might not be. The boolean field is actually the perfect example for that, it is a string one, aka a nice mix of 3 different data types.

7
  • 3
    just a reminder this is for a boolean field on a ECK bundle so it would be entity not node. When I did {{ kint(entity.field_boolean.value) }} the value was null. However if i use {{ kint(entity.field_boolean['#items'].value) }} then i get the correct output. Jan 26, 2018 at 23:31
  • 2
    Doesn't matter really as long as the variable is available in the template. I don't know how ECK does that, check the preprocess function. The standard is to have a content variable with the rendered fields and a $name_of_entity_type variable that is the entity with its field objects. This seems like strange naming. For the record, not a big fan of ECK, I'd rather recommend doing a custom entity type in 8.x, you can generate the starting point with drupal console. Which still has a few strange behaviors too. though
    – Berdir
    Jan 27, 2018 at 10:05
  • 1
    The advantage of a custom entity type is that you can define everything you need as base fields (or make it have a field UI for configurable fields if needed but often that's not the case) and make it revisionable/translatable only if needed, resulting in a minimum amount of tables and better save/load performance
    – Berdir
    Jan 27, 2018 at 10:06
  • 2
    @drupi17, the latest ECK alpha (a year old now) has indeed a strange naming scheme putting the rendered output in entity. The template is fixed now. So if you really want to use ECK then use the latest dev version, see this similar question drupal.stackexchange.com/questions/252362/…
    – 4uk4
    Jan 27, 2018 at 14:11
  • 1
    @4k4 I have already downloaded the latest version and will begin the testing with the template but ultimately using entity.field_boolean['#items'].value works Jan 29, 2018 at 16:27
8

Checkbox fields value check in Twig templates for drupal 8:

{% if node.field_checkbox['#items'].value == '1' %}
  <div class="only added when there are values">
    {{ content.field_name }}
  </div>
{% endif %}

Thats works fine for me.

5

If you create the Boolean field in the content type then this worked for me

{% if node.field_boolean.value == 1 %} {{ page.contact_form }} {% endif %}

and also if you have boolean field in a paragraph then this worked for me

{% if content.field_boolean['#items'].value == 1 %} {{ content.field_description }} {% endif %}

1
  • This helped me specifically with a boolean inside a paragraph.
    – LeraA
    Oct 8, 2021 at 20:19
4

For me, one test did not work consistently. However, the following condition seems to have covered all the use cases.

{% if content.field_boolean is not empty and node.field_boolean.value == '1' %}
0
0

For me too, to only safest is using both:

{% if content.field_boolean is not empty and node.field_boolean.value == '1' %}

Other cases might fail. You can find more information on this thread: https://www.drupal.org/project/drupal/issues/3203038

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.