class HtmlResponseAttachmentsProcessor implements AttachmentsResponseProcessorInterface (View source)

Processes attachments of HTML responses.

This class is used by the rendering service to process the #attached part of the render array, for HTML responses.

To render attachments to HTML for testing without a controller, use the 'bare_html_page_renderer' service to generate a Drupal\Core\Render\HtmlResponse object. Then use its getContent(), getStatusCode(), and/or the headers property to access the result.

Properties

protected AssetResolverInterface $assetResolver

The asset resolver service.

protected Config $config

A config object for the system performance configuration.

protected AssetCollectionRendererInterface $cssCollectionRenderer

The CSS asset collection renderer service.

protected AssetCollectionRendererInterface $jsCollectionRenderer

The JS asset collection renderer service.

protected RequestStack $requestStack

The request stack.

protected RendererInterface $renderer

The renderer.

protected ModuleHandlerInterface $moduleHandler

The module handler service.

Methods

__construct(AssetResolverInterface $asset_resolver, ConfigFactoryInterface $config_factory, AssetCollectionRendererInterface $css_collection_renderer, AssetCollectionRendererInterface $js_collection_renderer, RequestStack $request_stack, RendererInterface $renderer, ModuleHandlerInterface $module_handler)

Constructs a HtmlResponseAttachmentsProcessor object.

processAttachments(AttachmentsInterface $response)

Processes the attachments of a response that has attachments.

static string
formatHttpHeaderAttributes(array $attributes = [])

Formats an attribute string for an HTTP header.

renderPlaceholders(HtmlResponse $response)

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

array
processAssetLibraries(AttachedAssetsInterface $assets, array $placeholders)

Processes asset libraries into render arrays.

renderHtmlResponseAttachmentPlaceholders(HtmlResponse $response, array $placeholders, array $variables)

Renders HTML response attachment placeholders.

setHeaders(HtmlResponse $response, array $headers)

Sets headers on a response object.

array
processHtmlHead(array $html_head)

Ensure proper key/data order and defaults for renderable head items.

array
processHtmlHeadLink(array $html_head_link)

Transform a html_head_link array into html_head and http_header arrays.

array
processFeed(array $attached_feed)

Transform a 'feed' attachment into an 'html_head_link' attachment.

Details

__construct(AssetResolverInterface $asset_resolver, ConfigFactoryInterface $config_factory, AssetCollectionRendererInterface $css_collection_renderer, AssetCollectionRendererInterface $js_collection_renderer, RequestStack $request_stack, RendererInterface $renderer, ModuleHandlerInterface $module_handler)

Constructs a HtmlResponseAttachmentsProcessor object.

Parameters

AssetResolverInterface $asset_resolver

An asset resolver.

ConfigFactoryInterface $config_factory

A config factory for retrieving required config objects.

AssetCollectionRendererInterface $css_collection_renderer

The CSS asset collection renderer.

AssetCollectionRendererInterface $js_collection_renderer

The JS asset collection renderer.

RequestStack $request_stack

The request stack.

RendererInterface $renderer

The renderer.

ModuleHandlerInterface $module_handler

The module handler service.

AttachmentsInterface processAttachments(AttachmentsInterface $response)

Processes the attachments of a response that has attachments.

Libraries, JavaScript settings, feeds, HTML tags, HTML 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:

Parameters

AttachmentsInterface $response

The response to process.

Return Value

AttachmentsInterface

The processed response, with the attachments updated to reflect their final values.

Exceptions

InvalidArgumentException

static string formatHttpHeaderAttributes(array $attributes = [])

internal  
 

Formats an attribute string for an HTTP header.

Parameters

array $attributes

An associative array of attributes such as 'rel'.

Return Value

string

A ; separated string ready for insertion in a HTTP header. No escaping is performed for HTML entities, so this string is not safe to be printed.

See also

https://www.drupal.org/node/3000051

protected HtmlResponse renderPlaceholders(HtmlResponse $response)

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

HtmlResponse $response

The HTML response whose placeholders are being replaced.

Return Value

HtmlResponse

The updated HTML response, with replaced placeholders.

See also

Renderer::replacePlaceholders
Renderer::renderPlaceholder

protected array processAssetLibraries(AttachedAssetsInterface $assets, array $placeholders)

Processes asset libraries into render arrays.

Parameters

AttachedAssetsInterface $assets

The attached assets collection for the current response.

array $placeholders

The placeholders that exist in the response.

Return Value

array

An array keyed by asset type, with keys:

  • styles
  • scripts
  • scripts_bottom

protected renderHtmlResponseAttachmentPlaceholders(HtmlResponse $response, array $placeholders, array $variables)

Renders HTML response attachment placeholders.

This is the last step where all of the attachments are placed into the response object's contents.

Parameters

HtmlResponse $response

The HTML response to update.

array $placeholders

An array of placeholders, keyed by type with the placeholders present in the content of the response as values.

array $variables

The variables to render and replace, keyed by type with renderable arrays as values.

protected setHeaders(HtmlResponse $response, array $headers)

Sets headers on a response object.

Parameters

HtmlResponse $response

The HTML response to update.

array $headers

The headers to set, as an array. The items in this array should be as follows:

  • The header name.
  • The header value.
  • (optional) Whether to replace a current value with the new one, or add it to the others. If the value is not replaced, it will be appended, resulting in a header like this: 'Header: value1,value2'

protected array processHtmlHead(array $html_head)

Ensure proper key/data order and defaults for renderable head items.

Parameters

array $html_head

The ['#attached']['html_head'] portion of a render array.

Return Value

array

The ['#attached']['html_head'] portion of a render array with #type of html_tag added for items without a #type.

Transform a html_head_link array into html_head and http_header arrays.

Variable html_head_link is a special case of html_head which can be present as a link item in the HTML head section, and also as a Link: HTTP header, depending on options in the render array. Processing it can add to both the html_head and http_header sections.

Parameters

array $html_head_link

The 'html_head_link' value of a render array. Each head link is specified by a two-element array:

  • An array specifying the attributes of the link.
  • A boolean specifying whether the link should also be a Link: HTTP header.

Return Value

array

An ['#attached'] section of a render array. This allows us to easily merge the results with other render arrays. The array could contain the following keys:

  • http_header
  • html_head

protected array processFeed(array $attached_feed)

Transform a 'feed' attachment into an 'html_head_link' attachment.

The RSS feed is a special case of 'html_head_link', so we just turn it into one.

Parameters

array $attached_feed

The ['#attached']['feed'] portion of a render array.

Return Value

array

An ['#attached']['html_head_link'] array, suitable for merging with another 'html_head_link' array.