How to check a boolean value in twig templates?


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 }}
{% endif %}

Thats works fine for me.

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

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 }}
{% 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.

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 %}

Similar questions

How to test for a boolean field in a twig template?
field_boolean is a boolean field. {% if content.field_boolean == true %} or {% if content.field_boolean == 1 %} or {% if content.field_boolean == 'true' %} or {% if content.field_boolean == '1' %} don't work. How do I test if the checkbox is checked?
Twig theming boolean issue
I have a boolean for the status of a page on whether it is new or not. If it's selected, I want the following code to take effect. This works fine for when it's selected. However, if I unselect it, it still displays the div with the info--details__new class only empty. I'd like for this div to not be displayed when this boolean isn't selected but c...
Sorting a view basing on the value of a boolean field
I have a content type X. A view is used to display this content type (X), as a table. The content type has a field called "Preferred" which contains boolean values. I need to sort the table so that the "Preferred:true" items display at the top of the table. How can I achieve it?
How do I alter a boolean value and display a string in views output?
Whats the best way to override a simple 0/1 boolean field in views output so instead of 0/1 it would output "Hello" or "Goodbye"? EDIT: To improve on my question: Whats the best way to add further options to the default view boolean handler? For example ...
How do I change value of a boolean field on a node when a user clicks a button?
I am building a small site for a friend who is getting married and they have their gift list online. Each gift is a node which I am thinking will have a boolean field called something like "bought". The functionality I want to create is that visitors can look at a list of gifts and, if they want to buy something for them they just need to click a b...
How do I add a custom class to a field based on another field value(boolean)?
I have a Commerce product and there are two price fields. (Original price and Sale price). I would like to display original price as line-through text when sale price is available. Therefore I would like to add a custom class to a field based on another field value(boolean), I looked at this function, hook_entity_view_alter, but I don't know whethe...

Also ask

We use cookies to deliver the best possible experience on our website. By continuing to use this site, accepting or closing this box, you consent to our use of cookies. To learn more, visit our privacy policy.