Screenshot of the block confguration form with a new option: "hide this block if it has no content"

Experimental project

This is a sandbox project, which contains experimental code for developer use only.

Hide empty block is a means to avoid empty regions, a particular pain point in Drupal.

Conceptually, you can think of this as if it were a block visibility option:
"Show this block only if it produces actual content".

Many themes would like to control the layout of a page based on the content of regions, by doing things like this:
<body class="has-sidebar">...</body>

Lazy rendering of blocks muddies the water. A block is first rendered as a placeholder, and later on that placeholder is replaced with either content, or nothing. When the page template is evaluated, it only knows if a placeholder exists. If it does, then it must consider the region non-empty.

A concept like "has a sidebar" becomes "might have a sidebar".

Most of the time this is not a problem, but occasionally a theme will want to guarantee a region is not empty. In order to do that, any blocks within that region need to be evaluated ahead of time. This module lets you do that on a per-block basis.

Post-Installation

On the Block layout page at admin/structure/block, each block has a new hide empty block option.

This guarantee comes with a performance cost.
Blocks with this setting cannot make use of BigPipe and lazy building at all. Moreover, all the cache metadata of the block applied to the entire page, reducing the overall cacheability of that page.

Try to avoid using this module if at all possible. Before using this module, ask yourself two questions:

  1. can I restructure the template so that an empty block does not matter?
  2. can I use visibility conditions in the block's configuration instead?
Supporting organizations: 

Project information