Drupal 8  8.0.2
HtmlResponseAttachmentsProcessor Class Reference
Inheritance diagram for HtmlResponseAttachmentsProcessor:

Public Member Functions

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

Protected Member Functions

 renderPlaceholders (HtmlResponse $response)
 
 processAssetLibraries (AttachedAssetsInterface $assets, array $placeholders)
 
 renderHtmlResponseAttachmentPlaceholders (HtmlResponse $response, array $placeholders, array $variables)
 
 setHeaders (HtmlResponse $response, array $headers)
 
 processHtmlHead (array $html_head)
 
 processHtmlHeadLink (array $html_head_link)
 
 processFeed ($attached_feed)
 

Protected Attributes

 $assetResolver
 
 $config
 
 $cssCollectionRenderer
 
 $jsCollectionRenderer
 
 $requestStack
 
 $renderer
 
 $moduleHandler
 

Detailed Description

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 object. Then use its getContent(), getStatusCode(), and/or the headers property to access the result.

See Also
template_preprocess_html()

Constructor & Destructor Documentation

__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
\Drupal\Core\Asset\AssetResolverInterface$asset_resolverAn asset resolver.
\Drupal\Core\Config\ConfigFactoryInterface$config_factoryA config factory for retrieving required config objects.
\Drupal\Core\Asset\AssetCollectionRendererInterface$css_collection_rendererThe CSS asset collection renderer.
\Drupal\Core\Asset\AssetCollectionRendererInterface$js_collection_rendererThe JS asset collection renderer.
\Symfony\Component\HttpFoundation\RequestStack$request_stackThe request stack.
\Drupal\Core\Render\RendererInterface$rendererThe renderer.
\Drupal\Core\Extension\ModuleHandlerInterface$module_handlerThe module handler service.

References Drupal\config(), ConfigFactoryInterface\get(), Drupal\moduleHandler(), and Drupal\requestStack().

Here is the call graph for this function:

Member Function Documentation

processAssetLibraries ( AttachedAssetsInterface  $assets,
array  $placeholders 
)
protected

Processes asset libraries into render arrays.

Parameters
\Drupal\Core\Asset\AttachedAssetsInterface$assetsThe attached assets collection for the current response.
array$placeholdersThe placeholders that exist in the response.
Returns
array An array keyed by asset type, with keys:
  • styles
  • scripts
  • scripts_bottom

References Drupal\config(), and Drupal\state().

Referenced by HtmlResponseAttachmentsProcessor\processAttachments().

Here is the call graph for this function:

Here is the caller graph for this function:

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$responseThe response to process.
Returns
The processed response, with the attachments updated to reflect their final values.
Exceptions
\InvalidArgumentExceptionThrown when the $response parameter is not the type of response object the processor expects.
}

Implements AttachmentsResponseProcessorInterface.

References AttachedAssets\createFromRenderArray(), AttachmentsInterface\getAttachments(), EnforcedResponseException\getResponse(), BubbleableMetadata\mergeAttachments(), HtmlResponseAttachmentsProcessor\processAssetLibraries(), HtmlResponseAttachmentsProcessor\processFeed(), HtmlResponseAttachmentsProcessor\processHtmlHead(), HtmlResponseAttachmentsProcessor\processHtmlHeadLink(), HtmlResponseAttachmentsProcessor\renderHtmlResponseAttachmentPlaceholders(), HtmlResponseAttachmentsProcessor\renderPlaceholders(), Drupal\requestStack(), AttachmentsInterface\setAttachments(), and HtmlResponseAttachmentsProcessor\setHeaders().

Here is the call graph for this function:

processFeed (   $attached_feed)
protected

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_feedThe ['#attached']['feed'] portion of a render array.
Returns
array An ['#attached']['html_head_link'] array, suitable for merging with another 'html_head_link' array.

Referenced by HtmlResponseAttachmentsProcessor\processAttachments().

Here is the caller graph for this function:

processHtmlHead ( array  $html_head)
protected

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

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

Referenced by HtmlResponseAttachmentsProcessor\processAttachments().

Here is the caller graph for this function:

processHtmlHeadLink ( array  $html_head_link)
protected

Transform a html_head_link array into html_head and http_header arrays.

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_linkThe '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.
Returns
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

References Html\escape().

Referenced by HtmlResponseAttachmentsProcessor\processAttachments().

Here is the call graph for this function:

Here is the caller graph for this function:

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

Renders HTML response attachment placeholders.

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

Parameters
\Drupal\Core\Render\HtmlResponse$responseThe HTML response to update.
array$placeholdersAn array of placeholders, keyed by type with the placeholders present in the content of the response as values.
array$variablesThe variables to render and replace, keyed by type with renderable arrays as values.

References HtmlResponse\setContent().

Referenced by HtmlResponseAttachmentsProcessor\processAttachments().

Here is the call graph for this function:

Here is the caller graph for this function:

renderPlaceholders ( HtmlResponse  $response)
protected

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$responseThe HTML response whose placeholders are being replaced.
Returns
The updated HTML response, with replaced placeholders.
See Also
::replacePlaceholders()
::renderPlaceholder()

References BubbleableMetadata\createFromRenderArray(), AttachmentsInterface\getAttachments(), and HtmlResponse\setContent().

Referenced by HtmlResponseAttachmentsProcessor\processAttachments().

Here is the call graph for this function:

Here is the caller graph for this function:

setHeaders ( HtmlResponse  $response,
array  $headers 
)
protected

Sets headers on a response object.

Parameters
\Drupal\Core\Render\HtmlResponse$responseThe HTML response to update.
array$headersThe 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'

Referenced by HtmlResponseAttachmentsProcessor\processAttachments().

Here is the caller graph for this function:


The documentation for this class was generated from the following file: