I use the Views module to list the content of a custom entity, using the Table format.
I'd like to add the myClass CSS class to each row where a given field is equal to zero.

How can I achieve this?

Edit I found out that I could use 'Rewrite results' (Overriding the output of a field with custom text) with twig.
For example:

{% if nombreproduits == '0' %}  
{{ '99/99/9999' }}  
{% else %}  
{{ datedistribution }}  
{% endif %}  

How could I use that to add a class to the corresponding row?

2 Answers 2


Finally, I made it work this way:

In Page>Format>Settings, I set 'Row class' to "myClass"~{{ nombreproduits }} where nombreproduits is the field to be checked equal to zero.
The result is something like <tr class="myClass0"> in the corresponding rows (and, of course, myClass1, myClass2, etc. when nombreproduits = 1, 2 etc.).
Then, just have to add tr.myClass0to my custom CSS file...


Two options:

  1. You alter your view with a View hook and you add to the row class value your own class based on one of the field value
  2. You use this solution, and you can check with twig the actual row content and add a class based on that(use devel module and {{ kint(row) }} to understand how to reach the values you want)
  • Tried to use kint but, so far, without success. Will dig in...
    – gbmapo
    Jun 13, 2016 at 8:12
  • Twig is heavily cached, you will have to turn that off, else you always will have to clear the cache with drush cr
    – ssibal
    Jun 13, 2016 at 8:58
  • By the way, I'm not sure to know where to put {{ kint(row) }}. In one field 'Rewrite results'? Elsewhere?
    – gbmapo
    Jun 13, 2016 at 9:22
  • Well, you have a "for" loop {% for row in rows % }. You have to put it there. But you can try it anywhere to print out twig variables!
    – ssibal
    Jun 13, 2016 at 9:38

Your Answer

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

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