Score:1

How do get a value from field in if/else statement

jp flag

In a custom views-view-fields.html.twig I do print a list field (field_status) without issues. The values are:

new|New

old|Old

But if i want to do an if/else statement for printing different div containers i do get always printed the div container from else. Does anybody have an idea what i am doing wrong? I tried it too with module Twig Field Value, but i do have the same issues.

<div class="card h-100">
    <div class="card-body">
        {% if fields.field_status.content == 'new' %} // |striptags , |raw or a capital value e.g. New do not work , Twig Field Value also do not work: content.field_status|field_value
            <div class="icon medium">
                NEW
            </div>
        {% else %}
            <div class="icon medium">
                OLD
            </div>
        {% endif %}
        <h5 class="card-title">{{ fields.title.content }}</h5>
    </div>
</div>

Thanks for helping!

Score:3
cn flag

If you want to compare the left part of the field list you need the raw database value from the entity.

You find the entity in the row variable of views-view-fields.html.twig :

{% if row._entity.field_status.value == 'new' %}

See Access row value in template_preprocess_views_view_fields(&$variables)

druplash avatar
jp flag
You´re great, thanks a lot! Now it works without issues. I´ve spend hours and hours to find a solution.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.