0

I have a boolean for the status of a page on whether it is new or not. If it's selected, I want the following code to take effect.

{% if fields.field_new.content %}
  <div class="info--details__new">
    {{ fields.field_new.content }}
  </div>
{% endif %}

This works fine for when it's selected. However, if I unselect it, it still displays the div with the info--details__new class only empty.

I'd like for this div to not be displayed when this boolean isn't selected but can't seem to figure out how to do this in my twig template.

What's the best way to go about doing this so that my empty div doesn't show up when the boolean is unselected?

This is in my views-view-fields--VIEW-NAME.html.twig file.

4
  • And according to Berdir you don't even need the is not empty part.
    – Kevin
    Feb 5, 2018 at 16:35
  • @Kevin, I did come across that but it didn't work for me. Feb 5, 2018 at 16:40
  • Are you doing it at the field level or entity level? Oh, I see, you are doing it with Views.
    – Kevin
    Feb 5, 2018 at 17:22
  • Yes, this is a view block that lives on certain page types with a contextual filter associated with the content:ID Feb 5, 2018 at 18:43

2 Answers 2

2

As discussed in the linked topic use database values to check for booleans in a condition. In a View template you'll find for each row the entity the row is based on in row._entity, from which you can get the raw field values:

{% if row._entity.field_new.value %}
  <div class="info--details__new">
    {{ fields.field_new.content }}
  </div>
{% endif %}

Even if it works with debugging turned off, you still depend on how you've configured the rendered output and this is not good practice, if a small change in UI can break the code.

3
  • Thanks for this. So should I be doing if statements like this for all fields? Say I have a video field called field_video and I don't want that div to show if there is no video associated with the content, would I just replace the above field_new with field_video and that should work? Or is that specific to booleans? Feb 5, 2018 at 20:01
  • It's down to what's in the field and how Twig interprets that as truthy or otherwise. For example an empty string is false, so is a 0, but a string with whitespace is true. The same technique will work for most fields, but it's worth sanity-checking each type at least to be sure. There's a summary here: resources.shopwired.co.uk/coding/twig/truthy-and-falsy
    – Clive
    Feb 5, 2018 at 20:14
  • You've asked about a boolean. The entity provides typed data, for each field type a different one. If field_video is a reference to a media entity, then use .target_id instead of .value. Or use .isempty on the entity field, this method works for any field type.
    – 4uk4
    Feb 5, 2018 at 21:45
0

Well, this ended up being a debug issue. Because I had twig debug on, it still ends up displaying the div because of the debug code being printed for the field. Once I turn debug off, my code in the original question works as intended.

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