Score:0

How to prevent empty field wrapper from rendering in views

cn flag

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)

Score:1
kz flag

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;
}
Aambro avatar
cn flag
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.
I sit in a Tesla and translated this thread with Ai:

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.