Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
By Rewted on
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 %}
Comments
try ...
http://twig.sensiolabs.org/doc/2.x/tests/sameas.html
or http://drupal.stackexchange.com/questions/228389/how-to-test-for-a-boole...
No luck. I've tried these
Found another solution for anyone following this:
{% if node.field_warn.value == '1' %}
Tryyy
Hope you can try this {% if content.field_boolean == 1 %}
Single Quotes
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.
twig 1.x
Please keep in mind that Drupal 8-8.3 uses twig 1.x not 2.x so some functions are different.
For paragraphs and the "content" variable
Assuming you are setting the "On" value to "1."
The raw output of the field still has spaces so you need to do a "contains" like check.
This one works
This is the only solution that worked for my case.
I also confirm that only this
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
Thanks! After spending all
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
Another way...
Another way...
{% if content.field_myfield['#items'].getValue()|first.value == "1" %}
This worked for me.
This worked for me.
Worked for me
This worked for me after trying many things that didn't. Thank you!
Boolean or not so much?
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
The Below condition is
The Below condition is working for me
{% if "On" in content.field_boolean.0 %}
This one works for me on
This one works for me on drupal 9.2
Checkbox fields value check
Checkbox fields value check in Twig templates for drupal 8:
Getting the boolean value
Thanks, that worked, It's a nice solution because you get the boolean value stored in the DB.
This works for me too
This works for me too
Thank you
Thanks a lot for this
Thanks a lot for this solution. It is crazy how much time I spent on this issue.