Score:3

Prevent rendering of Layout Builder regions that have no content

in flag

Using Layout Builder, when you add a block to a layout that has access controls and a user visits a page without access, the layout is empty.

How does one prevent the layout from rendering at all if its contents are empty? While the {% if content.foo|render|striptags|trim %} trick works for block templates, I can't figure out how to get the same effect with layout templates.

The end result is I have extra markup in the page, which is classed and creating extra padding and vertical space between populated layout regions.

edit:

I have updated now with an event that Layout Builder needs:

class MyModuleEventSubscriber implements EventSubscriberInterface {

  /**
   * Layout Builder response event handler.
   *
   * @param SectionComponentBuildRenderArrayEvent $event
   *   Build render event.
   */
  public function onBuildRender(SectionComponentBuildRenderArrayEvent $event) {
    $id = $event->getPlugin()->getPluginDefinition()['id'];

    if ($id == 'inline_block' && !$event->inPreview()) {
      $build = $event->getBuild();
      $build['#create_placeholder'] = FALSE;
      $event->setBuild($build);
    }
  }

  /**
   * {@inheritdoc}
   */
  public static function getSubscribedEvents() : array {
    $events[LayoutBuilderEvents::SECTION_COMPONENT_BUILD_RENDER_ARRAY] = ['onBuildRender', 110];
    return $events;
  }

}

xdebug

enter image description here

Score:1
cn flag

This could be caused by placeholdering. Drupal formats placeholders as HTML tag. In your render test, striptags removes such placeholders, even if later, when the real rendering is happening, it is filled with a lazily-built block.

You can avoid it, at the expense of cache performance, by denying the block being placeholdered:

/**
 * Implements hook_block_build_BASE_BLOCK_ID_alter().
 */
function mymodule_block_build_example_block_alter(array &$build, \Drupal\Core\Block\BlockPluginInterface $block) {
  // Disable placeholdering of this block.
  $build['#create_placeholder'] = FALSE;
}

See Can BigPipe exclude certain blocks? This is not depending on BigPipe being enabled or not, but I think for both problems the solution is the same.

cn flag
Comments are not for extended discussion; this conversation has been [moved to chat](https://chat.stackexchange.com/rooms/129156/discussion-on-answer-by-4k4-prevent-rendering-of-layout-builder-regions-that-hav).
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.