-1

I have the following code in the views-view-fields--[VIEW NAME].html.twig template:

{% if fields.field_category.content == "Compare to this String" %}
    Conditional content here.
{% endif %}

This "category" field is an entity reference (Taxonomy) being output as plain text. If I replace the field in the code with the same string on the right side of the comparison operator, the conditional content is output correctly. When I try to compare this field to the string on the right side, Drupal outputs "The website encountered an unexpected error. Please try again later.".

What is the correct way to compare a views field with a String value using Twig and Drupal?

0

You can't compare rendered output to a string. In conditions use database values, which you get from the row entity:

row._entity.field_name.value

For a reference field get the referenced entity and the label, if you want to compare the name of the taxonomy term:

row._entity.field_category.entity.label

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.