The item.content for my field item is 'active'. However, when I use a comparison operator as seen below, it never equates to True but always False. I thought maybe some spacing issues was the reason, but that seems to not be the case.

Is the content in item.content not a string? I am assuming when I call item.content, its probably a object, but I can't find any documentation on how to turn a object's value into a string so it equates to true.

{% for item in items %}
  {% if item.content == 'active' %}
    <div{{ item.attributes.addClass('field__item', 'status_item_active') }}>{{ item.content }}</div>
  {% else %}
    <div{{ item.attributes.addClass('field__item', 'status_item_notactive') }}>{{ item.content }}</div>
  {% endif %}
{% endfor %}
  • This field should be a boolean I think when the field has two possible values. If you want to get it working as a text field then try to render before comparing, see drupal.stackexchange.com/questions/239105/…
    – 4uk4
    Commented May 23, 2019 at 14:00

2 Answers 2


You can try this instead

{% if element['#items'][key].value == 'active' %}
  • Yes, this is a good idea, comparing the database value from the field item instead of the rendered output. You need to get the key, for example by extending the loop definition: {% for key, item in items %}
    – 4uk4
    Commented May 24, 2019 at 7:40

I solved it with this code:

{% if 'active' in item.content|render|render %}
  • 1
    This is risky and unreliable condition, IMO. When you just want to read a value, you shouldn't have to render it out one or more times. See this answer: drupal.stackexchange.com/a/281520/57
    – Kevin
    Commented May 27, 2019 at 15:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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