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 crashbdx on
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}
{% endif %}
Thanks in advance :)
Comments
Checkboxes return boolean
Checkboxes return boolean value.
Pimp your Drupal 8 Toolbar - make it badass.
Adaptivetheme - theming system for people who don't code.
Thanks for your answer, yes
Thanks for your answer, yes about boolean but how properly test it with a if in twig ? I dont find it in twig doc.
...
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:
Seems I'm wrong about the vlaues being boolean here, looks like they're always strings actually.
Pimp your Drupal 8 Toolbar - make it badass.
Adaptivetheme - theming system for people who don't code.
Yes they do, does anyone know
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.
Something
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:
It's not clear what you are
It's not clear what you are commenting about.
Pimp your Drupal 8 Toolbar - make it badass.
Adaptivetheme - theming system for people who don't code.
How render value of checkbox label in twig?
For example if 1 is key and "On" is value then how render value of checkbox i.e. "On" in twig file.
Thanks!
Working fine with this.
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.
This approach did not work
This approach did not work for me, but the suggestion above worked like a charm:
For custom fielded blocks
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 %}
Perfect! This worked for me
Perfect! This worked for me in a block on a node page! Thank you!
Evaluating Multiple Checkbox values in D8 TWIG Email Templates
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:
(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.
alternatively if you wanted to just print the value of check, that would look like:
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.
D9
Could't find an answer for drupal 9 anywhere.
My solution is custom block twig:
here, the value actually is integer.