3

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

1

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.

1
  • Comments are not for extended discussion; this conversation has been moved to chat.
    – Clive
    Sep 1 at 12:23

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.