Using field_value in if statement

Created on 17 January 2023, almost 2 years ago

Problem/Motivation

I have a field that is a boolean field. I can print out the field value as "On" but when I try to use the value in an if statement, it does not evaluate correctly.

{% set display = 'no value' %}
{% set toggle = content.field_toggle|field_value %}
{% if toggle == "On" %}
  {% set display = 'elemental' %}
{% endif %} 

Toggle value {{ toggle }} Display value {{ display }}

This renders as Toggle value On Display value no value

If I instead use the regular method with {% if content.field_toggle.0["#markup"] == "On" %} then it works.

Steps to reproduce

Create a field that is a boolean with the default On/Off settings
Compare value as above

Proposed resolution

Remaining tasks

User interface changes

API changes

Data model changes

💬 Support request
Status

Active

Version

2.0

Component

Documentation

Created by

🇺🇸United States shelane

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • Status changed to Needs review over 1 year ago
  • 🇮🇳India sumit-k

    {{ content }} is a render array. on dump, we can see an array

    0 array:1 [▼
      0 => array:1 [▼
        "#markup" => "On"
      ]
    ]

    {{ content.field_toggle.0["#markup"] }}

    Content is not ideal for logic. use content. for rendering and node. for logic.

  • Status changed to Postponed: needs info about 1 month ago
  • 🇩🇪Germany Grevil

    Did that help you @shelane?

Production build 0.71.5 2024