HtmlResponseAttachmentsProcessor
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
Constructs a HtmlResponseAttachmentsProcessor object.
Processes the attachments of a response that has attachments.
Formats an attribute string for an HTTP header.
Processes asset libraries into render arrays.
Renders HTML response attachment placeholders.
Ensure proper key/data order and defaults for renderable head items.
Transform a html_head_link array into html_head and http_header arrays.
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.
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:
static string
formatHttpHeaderAttributes(array $attributes = [])
| internal |
Formats an attribute string for an HTTP header.
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.
protected array
processAssetLibraries(AttachedAssetsInterface $assets, array $placeholders)
Processes asset libraries into render arrays.
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.
protected
setHeaders(HtmlResponse $response, array $headers)
Sets headers on a response object.
protected array
processHtmlHead(array $html_head)
Ensure proper key/data order and defaults for renderable head items.
protected array
processHtmlHeadLink(array $html_head_link)
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.
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.