1

We have a field_boolean (boolean field) on an ECK Bundle on the article content type. While doing research we came across this question, but does not solve my problem.

What is the appropriate way to check for the boolean value?

We have attempted but they don't work:

{% if entity.field_boolean == True %}
{% if '1' in entity.field_boolean[0] %}

These appear to work, but not sure it is correct approach.

{% if entity.field_boolean[#items].value == '1' %}
{% if entity.field_boolean[#items].value %}
  • Did you try {% if node.field_foo.value === ... %}? – Kevin Jan 25 '18 at 18:07
  • @Kevin Yes, but i got the following error message "Twig_Error_Syntax: Unexpected token "operator" of value "=". in Twig_ExpressionParser->parsePrimaryExpression()", which did not make sense to me. – usernameabc Jan 25 '18 at 18:54
  • I'm sorry, either == or same as should work. same as is like ===. twig.symfony.com/doc/2.x/tests/sameas.html – Kevin Jan 25 '18 at 18:57
  • @Kevin when i tried that it would always be true – usernameabc Jan 26 '18 at 18:50
7

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

https://www.md-systems.ch/en/blog/techblog/2017/02/20/twig-and-entity-field-api-or-how-to-take-control-of-nodehtmltwig

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

  • 1
    just a reminder this is for a boolean field on a ECK bundle so it would be entity not node. When I did {{ kint(entity.field_boolean.value) }} the value was null. However if i use {{ kint(entity.field_boolean['#items'].value) }} then i get the correct output. – usernameabc Jan 26 '18 at 23:31
  • Doesn't matter really as long as the variable is available in the template. I don't know how ECK does that, check the preprocess function. The standard is to have a content variable with the rendered fields and a $name_of_entity_type variable that is the entity with its field objects. This seems like strange naming. For the record, not a big fan of ECK, I'd rather recommend doing a custom entity type in 8.x, you can generate the starting point with drupal console. Which still has a few strange behaviors too. though – Berdir Jan 27 '18 at 10:05
  • The advantage of a custom entity type is that you can define everything you need as base fields (or make it have a field UI for configurable fields if needed but often that's not the case) and make it revisionable/translatable only if needed, resulting in a minimum amount of tables and better save/load performance – Berdir Jan 27 '18 at 10:06
  • @drupi17, the latest ECK alpha (a year old now) has indeed a strange naming scheme putting the rendered output in entity. The template is fixed now. So if you really want to use ECK then use the latest dev version, see this similar question drupal.stackexchange.com/questions/252362/… – 4k4 Jan 27 '18 at 14:11
  • @4k4 I have already downloaded the latest version and will begin the testing with the template but ultimately using entity.field_boolean['#items'].value works – usernameabc Jan 29 '18 at 16:27
0

For me, one test did not work consistently. However, the following condition seems to have covered all the use cases.

{% if content.field_boolean is not empty and node.field_boolean.value == '1' %}
0

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

Thats works fine for me.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.