0

In views I display fields for specific content types. One field is referenced to taxonomy term. Not every node has the term reference. I need to style this field with paddings and colours so I would want it not to be rendered at all if term reference is missing. I have chosen under "no result behaviour" in field settings in views to "hide if empty" and "hide rewriting if empty". However, the two divs still get rendered in HTML at empty fields.. (.views-field and inside .field-content).

Is this expected behaviour? I tried to fix it with twig template (views-view-fields), but I can't seem to find solution of how to check for empty content (null or empty don't work on field.content as it returns length>0, I suppose the html is already in field.content)

1 Answer 1

1

It may seem a bit ugly solution, but if you wanna avoid a template override you can archive this effect simply by using the css selector :empty on the divs, something like:

div.class:empty {
  display: none;
}
1
  • Thanks, this didn't work because I had some twig debug comments inside field and within them some whitespace characters. But you set me on the right track. I removed twig debug temporarily and then views started hiding whole field's html wrapper as designed. So twig debugger somehow prevented views to hide the field even if "empty". If I added some text under "no results behaviour", it worked even with twig debug enabled. So at some point views realized it's empty. It just didn't hide HTML.
    – Aambro
    Commented Jul 13, 2023 at 9:04

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.