For a view displaying an optional field, 'hide if empty' doesn't work where a node matches the view query but the field is empty

Created on 10 January 2018, almost 7 years ago
Updated 25 January 2023, almost 2 years ago

Problem/Motivation

I have created a view for a content type to display an attached (unlimited cardinality) field. The node does not contain a value in the attached field.

I have set "Hide block if the view output is empty" to Yes and for the field (field_acknowledgements), in no results behaviour - hide if empty and hide rewriting if empty.

The generated SQL looks as follows:

SELECT node__field_acknowledgements.delta AS node__field_acknowledgements_delta, node__field_acknowledgements.langcode AS node__field_acknowledgements_langcode, node__field_acknowledgements.bundle AS node__field_acknowledgements_bundle, node__field_acknowledgements.field_acknowledgements_value AS node__field_acknowledgements_field_acknowledgements_value, node_field_data.nid AS nid
FROM 
{node_field_data} node_field_data
LEFT JOIN {node__field_acknowledgements} node__field_acknowledgements ON node_field_data.nid = node__field_acknowledgements.entity_id AND node__field_acknowledgements.deleted = '0'
WHERE ((node_field_data.nid = '21' )) AND ((node_field_data.status = '1') AND (node_field_data.type IN ('health')))

Steps to reproduce

  1. Install Drupal 9, standard profile
  2. Add an optional field to the Article node type
  3. Create an article with a title and no value in the field
  4. Create a view to show article fields, show only the new field
  5. Create a block display of the view, turn on Advanced -> Hide block if the view output is empty
  6. Place the block in a region
  7. Go to the home page - the block will still be there but have no visible content (you can see the block in the DOM)
🐛 Bug report
Status

Active

Version

10.1

Component
Views 

Last updated about 2 hours ago

Created by

🇬🇧United Kingdom welly

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • First commit to issue fork.
  • 🇮🇳India Meeni_Dhobale

    I tried to solve it, and spend some time but, but didn't understand why this happening, this issue not coming in Drupal core 9.5. I compared both files and view for the same blocks in both Drupal versions but didn't see any difference. Not understanding why this happening, If someone has any idea regarding it please share.

  • 🇦🇺Australia darvanen Sydney, Australia

    @Meeni_Dhobale are you saying that you followed the steps to reproduce using 9.5.x and were not able to reproduce the problem? Can I check that you inspected the HTML output of the page, not just what the browser printed on the screen?

  • 🇮🇹Italy finex

    Hi, after a bit of testing I realized that, in my case, the problem was related to twig debugging. In fact, in the development environment I have twig debugging active and therefore the empty fields are also visible, while in production, with debugging deactivated, the empty fields are correctly hidden.

  • 🇮🇳India anand.toshniwal93

    After attempting to replicate the scenario, I discovered that disabling twig debug resolves the issue, resulting in everything functioning as expected. This aligns with the observation mentioned in the previous comment 🐛 For a view displaying an optional field, 'hide if empty' doesn't work where a node matches the view query but the field is empty Active , where the problem was rectified by turning off twig debugging.

    Tried with Drupal: 9.5.11

  • 🇮🇳India Vivek Panicker Kolkata

    After reading the above comment, I have a question that shouldn't the original field value be checked before rendering, instead of waiting for the render phase to check if the field is empty or not?

  • 🇬🇧United Kingdom donaldp

    I've just hit this issue or similar and can confirm that it only appears with twig debugging enabled.
    This was happening in a view using views_accordion showing 3 fields for each row, one of which was empty. Despite having "Hide if empty" and "Hide rewriting if empty" the field heading still appeared when twig debugging was enabled. This was both in the views preview and the view page.

    Running Drupal 10.3.2 with all modules up to date.

  • 🇺🇸United States phillamb168

    Just to confirm what finex and anand.toshniwal93 have said - disabling twig debugging fixes this.

Production build 0.71.5 2024