In the html.html.twig file of my theme if added the following code to body_classes to add a body class if a region is not empty:

page.hero ? 'w-hero-bg',

So it looks like this:

  set body_classes = [
    logged_in ? 'user-logged-in',
    not root_path ? 'path-frontpage' : 'path-' ~ root_path|clean_class,
    node_type ? 'page-node-type-' ~ node_type|clean_class,
    db_offline ? 'db-offline',
    page.hero ? 'w-hero-bg',

I've created a block with views containing an 'Hero image'. I put a filter in place which checks if there is a media id available in the 'Hero image' field. If there is none available, the block is empty and is not displayed.

I've placed this block in the Hero region (page.hero). But even when the block is not displayed, and the region is not rendered in the DOM the class 'w-hero-bg' is still being attached to the body.

Is this a bug or am I missing something?

https://www.drupal.org/project/drupal/issues/953034 might be relevant, but I can't determine if this is related as my region is not rendered in the DOM.

  • Does this happen when you have twig debugging disabled? – Ryan Hartman Jun 2 at 13:00
  • Yes, if happens when twig debugging is disabled. – rroose Jun 3 at 9:32
  • How do you determine if the block is displayed? – Sébastien Gicquel Jun 8 at 11:03
  • I'm placing the block in the hero region on all pages/nodes. But I'm also filtering out nodes that don't have a value for the 'Hero image' field in the View. So technically the block is displayed on all pages but shouldn't be displayed on pages that don't have a 'Hero image'. The DOM doesn't render the block, but it looks like Drupal still thinks there is a block. If I display the block only for certain nodes or content types than the code in the twig file works, but I want it to work based on the input of the hero image field. – rroose Jun 9 at 8:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.