You are here

protected function AmpHtmlResponseAttachmentsProcessor::renderPlaceholders in Accelerated Mobile Pages (AMP) 8

Renders placeholders (#attached['placeholders']).

First, the HTML response object is converted to an equivalent render array, with #markup being set to the response's content and #attached being set to the response's attachments. Among these attachments, there may be placeholders that need to be rendered (replaced).

Next, RendererInterface::renderRoot() is called, which renders the placeholders into their final markup.

The markup that results from RendererInterface::renderRoot() is now the original HTML response's content, but with the placeholders rendered. We overwrite the existing content in the original HTML response object with this markup. The markup that was rendered for the placeholders may also have attachments (e.g. for CSS/JS assets) itself, and cacheability metadata that indicates what that markup depends on. That metadata is also added to the HTML response object.

Parameters

\Drupal\Core\Render\HtmlResponse $response: The HTML response whose placeholders are being replaced.

Return value

\Drupal\Core\Render\HtmlResponse The updated HTML response, with replaced placeholders.

See also

\Drupal\Core\Render\Renderer::replacePlaceholders()

\Drupal\Core\Render\Renderer::renderPlaceholder()

1 call to AmpHtmlResponseAttachmentsProcessor::renderPlaceholders()
AmpHtmlResponseAttachmentsProcessor::processAttachments in src/Render/AmpHtmlResponseAttachmentsProcessor.php
Processes the attachments of a response that has attachments.

File

src/Render/AmpHtmlResponseAttachmentsProcessor.php, line 321
Contains \Drupal\amp\Render\AmpHtmlResponseAttachmentsProcessor.

Class

AmpHtmlResponseAttachmentsProcessor
Processes attachments of AMP HTML responses.

Namespace

Drupal\amp\Render

Code

protected function renderPlaceholders(HtmlResponse $response) {
  $build = [
    '#markup' => Markup::create($response
      ->getContent()),
    '#attached' => $response
      ->getAttachments(),
  ];

  // RendererInterface::renderRoot() renders the $build render array and
  // updates it in place. We don't care about the return value (which is just
  // $build['#markup']), but about the resulting render array.
  // @todo Simplify this when https://www.drupal.org/node/2495001 lands.
  $this->renderer
    ->renderRoot($build);

  // Update the Response object now that the placeholders have been rendered.
  $placeholders_bubbleable_metadata = BubbleableMetadata::createFromRenderArray($build);
  $response
    ->setContent($build['#markup'])
    ->addCacheableDependency($placeholders_bubbleable_metadata)
    ->setAttachments($placeholders_bubbleable_metadata
    ->getAttachments());
  return $response;
}