You are here

public function HtmlResponseAttachmentsProcessor::processAttachments in HTTP/2 Server Push 8

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 AttachmentsResponseProcessorInterface::processAttachments

File

src/Render/HtmlResponseAttachmentsProcessor.php, line 47

Class

HtmlResponseAttachmentsProcessor
Decorates the HTML response attachments processor service, adds Server Push.

Namespace

Drupal\http2_server_push\Render

Code

public function processAttachments(AttachmentsInterface $response) {
  $response = $this->htmlResponseAttachmentsProcessor
    ->processAttachments($response);
  $request = $this->requestStack
    ->getCurrentRequest();
  if ($request->attributes
    ->has('http2_server_push_link_headers')) {
    $new_link_headers = $request->attributes
      ->get('http2_server_push_link_headers');
    $existing_link_headers = $response->headers
      ->get('Link', NULL, FALSE);
    $merged_link_headers = array_unique(array_merge($existing_link_headers, $new_link_headers));
    $response->headers
      ->set('Link', $merged_link_headers, TRUE);
  }
  return $response;
}