You are here

protected function HtmlResponseAttachmentsProcessor::renderPlaceholders in Drupal 10

Same name and namespace in other branches
  1. 8 core/lib/Drupal/Core/Render/HtmlResponseAttachmentsProcessor.php \Drupal\Core\Render\HtmlResponseAttachmentsProcessor::renderPlaceholders()
  2. 9 core/lib/Drupal/Core/Render/HtmlResponseAttachmentsProcessor.php \Drupal\Core\Render\HtmlResponseAttachmentsProcessor::renderPlaceholders()

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()

File

core/lib/Drupal/Core/Render/HtmlResponseAttachmentsProcessor.php, line 270

Class

HtmlResponseAttachmentsProcessor
Processes attachments of HTML responses.

Namespace

Drupal\Core\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;
}