-1

Is there a variable in views-view-fields.html.twig that contains the field machine name, or some other unique indicator? I am attempting something like this:

{% if field.[field_machine_name] == "my_field_machine_name" %}
   // Do stuff
{% endif %}

A massive workaround would be to create individual views.view.field--[my_view]--[my_field].html.twig templates for every field in the view, but it really seems like that shouldn't be necessary.

2 Answers 2

0

I ended up accomplishing this through the views UI by setting all of my fields, except for the last one, to "hidden" and then rewriting the output of the last field to include the hidden fields, and including the markup that I want.

0

How to Render Individual Fields in Views Templates: views-view-fields.html.twig

I've created template for views-view-fields.html.twig.
For this I used some fields values as condition to set css classes and conditionally display div elements ect.

so I have set conditions via if statements almost the same as stated in createdbycocoon

{% if fields.field_custom.content|striptags|trim is not empty %} and it worked nicely but when i need get value of field and check its value (in my case it list text field type and i need the key of the list item) it won't work if you'll try to do it. like this sample

{% if (fields.uid.content|striptags) == 'John' %}

so I had my research and figure out how to do it

I did it like this:

{% set bg_type = row._entity.field_background_type.value|striptags|trim %}
{% if bg_type == 'KEY_VALUE' %}
...
{% endif %}

Basically row._entity.field_background_type.value|striptags|trim helped to get value and check it...

I hope it will help to some one ...

1
  • OP was asking for field named, not field values. I think your answer is probably right, but it's actually an answer to a different question.
    – leymannx
    Commented Oct 20, 2022 at 21:56

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.