Problem/Motivation

Steps to repeat:

  1. Install a vanila D8 site
  2. Create a default view and literally keep the defaults
  3. Add a view block with the defaults still
  4. Under the ADVANCED pane, set Hide block if the view output is empty to Yes
  5. Save the view
  6. Go to Structure > Block Layout
  7. Place the block that you have created in the Sidebar Second region. This view block will also be the only one in this region
  8. Go to the site's homepage

Screenshot of the Views block configuration:
Drupal core Views block configuration

Expected Result:
The empty view block which was specified to not be displayed when it's empty should not trigger the region to be rendered.

Actual Result:
The empty view block caused page.html.twig's ` if page.second_sidebar ` condition to be evaluated as TRUE with literally nothing inside it at all like:
Drupal core Views block sidebar trigger

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

leolando.tan created an issue. See original summary.

Lendude’s picture

Status: Active » Closed (duplicate)
timhtheos’s picture

Hi @leolando.tan,

In your case, the view itself didn't render. If you want the region not to render itself if it is empty, try to pipe your condition to | render. Your {% if page.second_sidebar %} should be {% if page.second_sidebar | render %}.

leolandotan’s picture

Hi @Lendude and @timhtheos, thank you very much for your responses! Your explanations and solutions really helped me. This looks like a quite serious issue.

mindhunter75’s picture

I ran into the same problem. I tried the "| render" options.
The sidebar is empty, but then the bootstrap grids don't match the 12cols anymore.

Normal with no sidebar i have:

9col - 3col.

But now i have

6col - 3col ....

So i have a space in my theme.

Does anyone have a solution? I use the Bootstrap theme.

Thank you in advance.

jwilson3’s picture

Cross-post from #2443457. Just following up here that the current workaround until #953034: [meta] Themes improperly check renderable arrays when determining visibility lands is to first render the output of the region the block is placed in to determine whether it is empty or not by stripping out the rendered tags.

Eg. in page.html.twig in your theme:

{# render and strip out everything except images, iframes, objects and svgs that don't have any inner html #}
{% set has_second_sidebar = page.second_sidebar|render|striptags('<img><iframe><object><svg>')|trim|length %}

...

{% if has_second_sidebar %}
  {{ page.second_sidebar }}
{% endif %}

...
jwilson3’s picture