function HtmlResponseBigPipeSubscriber::onRespondEarly

Same name in other branches
  1. 9 core/modules/big_pipe/src/EventSubscriber/HtmlResponseBigPipeSubscriber.php \Drupal\big_pipe\EventSubscriber\HtmlResponseBigPipeSubscriber::onRespondEarly()
  2. 8.9.x core/modules/big_pipe/src/EventSubscriber/HtmlResponseBigPipeSubscriber.php \Drupal\big_pipe\EventSubscriber\HtmlResponseBigPipeSubscriber::onRespondEarly()
  3. 11.x core/modules/big_pipe/src/EventSubscriber/HtmlResponseBigPipeSubscriber.php \Drupal\big_pipe\EventSubscriber\HtmlResponseBigPipeSubscriber::onRespondEarly()

Adds markers to the response necessary for the BigPipe render strategy.

Parameters

\Symfony\Component\HttpKernel\Event\ResponseEvent $event: The event to process.

File

core/modules/big_pipe/src/EventSubscriber/HtmlResponseBigPipeSubscriber.php, line 44

Class

HtmlResponseBigPipeSubscriber
Response subscriber to replace the HtmlResponse with a BigPipeResponse.

Namespace

Drupal\big_pipe\EventSubscriber

Code

public function onRespondEarly(ResponseEvent $event) {
    $response = $event->getResponse();
    if (!$response instanceof HtmlResponse) {
        return;
    }
    // Wrap the scripts_bottom placeholder with a marker before and after,
    // because \Drupal\big_pipe\Render\BigPipe needs to be able to extract that
    // markup if there are no-JS BigPipe placeholders.
    // @see \Drupal\big_pipe\Render\BigPipe::sendPreBody()
    $attachments = $response->getAttachments();
    if (isset($attachments['html_response_attachment_placeholders']['scripts_bottom'])) {
        $scripts_bottom_placeholder = $attachments['html_response_attachment_placeholders']['scripts_bottom'];
        $content = $response->getContent();
        $content = str_replace($scripts_bottom_placeholder, '<drupal-big-pipe-scripts-bottom-marker>' . $scripts_bottom_placeholder . '<drupal-big-pipe-scripts-bottom-marker>', $content);
        $response->setContent($content);
    }
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.