Hi everyone, i do have a form associated to my content type, i added a checkbox in this form so when i add an entry i can check the checkbox if i want display a certain image on my template or not. How do i do the test like

{% if content.field_myCheckbox is checked}

Only local images are allowed.

{% endif %}

Thanks in advance :)

Comments

crashbdx’s picture

Thanks for your answer, yes about boolean but how properly test it with a if in twig ? I dont find it in twig doc.

Jeff Burnz’s picture

Through the entity object, in this case Node.

First set the Field Display to appropriate output, I am going to use 1/0 and test on that in twig:

    {% if node.field_mycheckbox.value == '1' %}
      <p>print 1</p>
    {% else %}
      <p>print 0</p>
    {% endif %}

Seems I'm wrong about the vlaues being boolean here, looks like they're always strings actually.

jaarong’s picture

Yes they do, does anyone know why? Why aren't the labels and corresponding boolean values separated in the field?

I'm finding the same thing with the list field. If you select it, I see no way to populate the list with options. Seems like I have some core misunderstanding with how this is supposed to work.

Baileykos’s picture

In fact, if I do not comment the query_builder part, it will only generate the checked part, but if I do this Symphony will check the integrity of the original array and the submit array. I can not add any new activity. In fact I generate the choice by java script like this:

 <div class="activity_checkbox" data-prototype='<div>
 <input type="checkbox" checked="checked" value="100" placeholder="" name="acme_prospection_company[activity][]">
 <label></label></div>'>
Jeff Burnz’s picture

ruchirapingale’s picture

For example if 1 is key and "On" is value then how render value of checkbox i.e. "On" in twig file.

Thanks!

Damodharan K’s picture

Hi, I am using boolean variable checking with my twig,
For "On" I changed the label to "Yes", after I am checking through this code

{% if pro_lay|striptags|trim == "Yes" %}
//code here...
{% endif %}

Thanks.

makbeta’s picture

This approach did not work for me, but the suggestion above worked like a charm:

{% if node.field_mycheckbox.value == '1' %}
michelledarling’s picture

For a custom block with a boolean field, nothing above worked for me because I don't know what entity object I'd use for a custom block. This worked:

{% if content.field_display_alert[0]|render == "On" %}
<div class="container alert-wrapper">
{{ content.field_heading }}
{{ content.field_link }}
</div>
{% endif %}

hans.p.’s picture

Perfect! This worked for me in a block on a node page! Thank you!

rex.barkdoll’s picture

Hi all, I wanted to add my two cents which may not directly relate to this post, but it was the only one that came up while I was searching for what I needed.

I'm working with D8 Webform Email templates using a TWIG body so that I can add logic. It's wonderful, but today I was trying to figure out how to get the value of multiple checkboxes that were part of the same grouping.

My check boxes in the form look like this: 

<input data-drupal-selector="edit-medicine-0" class="form-checkbox" type="checkbox" id="edit-medicine-0" name="medicine[0]" value="0">
<input data-drupal-selector="edit-medicine-4" class="form-checkbox" type="checkbox" id="edit-medicine-4" name="medicine[4]" value="4">

(basically the values for these two are 0 and 4 in the Form Builder in the back end.)

In order to get the values from the data.medicine array which stores all the checkbox values, you'll need to use a TWIG  FOR loop to iterate through the array/object and get each value. (data is the object that stores all the form key value pairs, data.medicine is one of the object keys which is storing an array of the checkbox values, while something like data.my_name would probably just hold a single value like "Rex" rather than an array of values).

Then, if you want to spit out different text in relation to what each value means, you write an IF statement to evaluate the value of check compared to the value of the input you set up and then print your text.

{% for  check in data.medicine %}
   {% if  check == 0 %} None{% endif %}
   {% if  check == 4 %} The Basics{% endif %}
   {% if  check == 10 %}Good{% endif %}
   {% if  check == 15 %}Very Good{% endif %}
{% endfor %}

alternatively if you wanted to just print the value of check, that would look like:

{{ check }}

Again, my apologies if this isn't directly related to this question/post, but this took me WAY too long to figure out that checkbox values are stored in an array so they need to be evaluated in this way to get out values. Radios, text fields, and number fields are much simpler. I suspect that Select Multiple fields may also need this approach.

tinytina’s picture

Could't find an answer for drupal 9 anywhere.

My solution is custom block twig:

{% if content.field_checkbox.0["#markup"] == 1 %}
	{# do something when checked#}
{% endif %}

here, the value actually is integer.