I've created a Boolean field (field_warn) with the settings listed below. I expect the message to display if On (true) or display nothing if Off, but the message is ALWAYS displayed regardless if the box is checked (on) or unchecked (off). Any ideas why?

  • Allowed number of values of 1
  • "On" label: On
  • "Off" label: Off

In my .twig file:

{% if content.field_warn == true %}
   <div aria-label="message">
    This is a warning.
   </div>
{% endif %}

Reference: http://twig.sensiolabs.org/doc/2.x/tags/if.html

Comments

Rewted’s picture

Found another solution for anyone following this:

{% if node.field_warn.value == '1' %}

truptidhal’s picture

Hope you can try this {% if content.field_boolean == 1 %}

robert_t_taylor’s picture

For anyone having issues here, the key to this test is to use single quotes. If you use double quotes you will be testing equivalence to a string which will always be false.  

It's a subtle error that will drive you bonkers.

HongPong’s picture

Please keep in mind that Drupal 8-8.3 uses twig 1.x not 2.x so some functions are different.

jds1’s picture

Assuming you are setting the "On" value to "1."

{% if '1' in content.field_name[0] %}
<div class="stuff">
do stuff
</div>
{% endif %}

The raw output of the field still has spaces so you need to do a "contains" like check.

randell’s picture

This is the only solution that worked for my case.

dco’s picture

I also confirm that only this one is working for boolean values.
My field is configured to show True or False values and the {% if 'True' in content.field_name[0] %} is working as expected

Drupal 8.7.1

JCL324’s picture

Thanks! After spending all day, this is the only thing that worked. However, I still hate having to hard code 'True' (in my case 'On') based on the display formatter that someone could change and break it all.

JCL

harcher’s picture

Another way...

{% if content.field_myfield['#items'].getValue()|first.value == "1" %}

kregan’s picture

This worked for me.

tylired’s picture

This worked for me after trying many things that didn't. Thank you!

Greenman77’s picture

I fumbled around with this myself for hours dumping variables out to see what was going on. Even tried changing the "ON"/"OFF" to "TRUE"/"FALSE" my data was in content.field_overlay.0 and should have evaluated just in a {% if content.field_overlay.0 %}.

What worked for me in the end was the below which I'd expect this is not boolean comparison but a string despite the field being set to boolean.

{% if "TRUE" in content.field_overlay.0 %}

This blog article was of some help https://blog.usejournal.com/getting-drupal-8-field-values-in-twig-22b80cb609bd

Jabastin Arul’s picture

The Below condition is working for me

{% if "On" in content.field_boolean.0 %}

blu_regard’s picture

This one works for me on drupal 9.2

Rabeeca’s picture

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 %}
pierregermain’s picture

Thanks, that worked, It's a nice solution because you get the boolean value stored in the DB.

Alina Basarabeanu’s picture

This works for me too
Thank you

kimble’s picture

Thanks a lot for this solution. It is crazy how much time I spent on this issue.