0

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

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 %} – 4k4 May 24 at 7:40
0

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 May 27 at 15:14

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.