BigPipeResponseAttachmentsProcessor::processAttachments

public BigPipeResponseAttachmentsProcessor::processAttachments(AttachmentsInterface $response)

Processes the attachments of a response that has attachments.

Libraries, JavaScript settings, feeds, HTML <head> tags, HTML <head> links, HTTP headers, and the HTTP status code are attached to render arrays using the #attached property. The #attached property is an associative array, where the keys are the attachment types and the values are the attached data. For example:

$build['#attached']['library'][] = [
  'library' => ['core/jquery']
];
$build['#attached']['http_header'] = [
  ['Content-Type', 'application/rss+xml; charset=utf-8'],
];

The available keys are:

  • 'library' (asset libraries)
  • 'drupalSettings' (JavaScript settings)
  • 'feed' (RSS feeds)
  • 'html_head' (tags in HTML <head>)
  • 'html_head_link' (<link> tags in HTML <head>)
  • 'http_header' (HTTP headers and status code)

Parameters

\Drupal\Core\Render\AttachmentsInterface $response: The response to process.

Return value

\Drupal\Core\Render\AttachmentsInterface The processed response, with the attachments updated to reflect their final values.

Throws

\InvalidArgumentException Thrown when the $response parameter is not the type of response object the processor expects.

Overrides HtmlResponseAttachmentsProcessor::processAttachments

File

core/modules/big_pipe/src/Render/BigPipeResponseAttachmentsProcessor.php, line 59

Class

BigPipeResponseAttachmentsProcessor
Processes attachments of HTML responses with BigPipe enabled.

Namespace

Drupal\big_pipe\Render

Code

public function processAttachments(AttachmentsInterface $response) {
  assert('$response instanceof \Drupal\Core\Render\HtmlResponse');

  // First, render the actual placeholders; this will cause the BigPipe
  // placeholder strategy to generate BigPipe placeholders. We need those to
  // exist already so that we can extract BigPipe placeholders. This is hence
  // a bit of unfortunate but necessary duplication.
  // @see \Drupal\big_pipe\Render\Placeholder\BigPipeStrategy
  // (Note this is copied verbatim from
  // \Drupal\Core\Render\HtmlResponseAttachmentsProcessor::processAttachments)
  try {
    $response = $this->renderPlaceholders($response);
  }
  catch (EnforcedResponseException $e) {
    return $e->getResponse();
  }

  // Extract BigPipe placeholders; HtmlResponseAttachmentsProcessor does not
  // know (nor need to know) how to process those.
  $attachments = $response->getAttachments();
  $big_pipe_placeholders = [];
  $big_pipe_nojs_placeholders = [];
  if (isset($attachments['big_pipe_placeholders'])) {
    $big_pipe_placeholders = $attachments['big_pipe_placeholders'];
    unset($attachments['big_pipe_placeholders']);
  }
  if (isset($attachments['big_pipe_nojs_placeholders'])) {
    $big_pipe_nojs_placeholders = $attachments['big_pipe_nojs_placeholders'];
    unset($attachments['big_pipe_nojs_placeholders']);
  }
  $html_response = clone $response;
  $html_response->setAttachments($attachments);

  // Call HtmlResponseAttachmentsProcessor to process all other attachments.
  $processed_html_response = $this->htmlResponseAttachmentsProcessor->processAttachments($html_response);

  // Restore BigPipe placeholders.
  $attachments = $processed_html_response->getAttachments();
  $big_pipe_response = clone $processed_html_response;
  if (count($big_pipe_placeholders)) {
    $attachments['big_pipe_placeholders'] = $big_pipe_placeholders;
  }
  if (count($big_pipe_nojs_placeholders)) {
    $attachments['big_pipe_nojs_placeholders'] = $big_pipe_nojs_placeholders;
  }
  $big_pipe_response->setAttachments($attachments);

  return $big_pipe_response;
}
doc_Drupal
2016-10-29 08:46:18
Comments
Leave a Comment

Please login to continue.