public function DfpHtmlResponseAttachmentsProcessor::processAttachments in Doubleclick for Publishers (DFP) 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 HtmlResponseAttachmentsProcessor::processAttachments
File
- src/
DfpHtmlResponseAttachmentsProcessor.php, line 83 - Contains \Drupal\dfp\DfpResponseAttachmentsProcessor.
Class
- DfpHtmlResponseAttachmentsProcessor
- Processes attachments of HTML responses with Dfp slot attachments.
Namespace
Drupal\dfpCode
public function processAttachments(AttachmentsInterface $response) {
// @todo Convert to assertion once https://www.drupal.org/node/2408013 lands
if (!$response instanceof HtmlResponse) {
throw new \InvalidArgumentException('\\Drupal\\Core\\Render\\HtmlResponse instance expected.');
}
// First, render the actual placeholders. This may add attachments so this
// is a bit of unfortunate but necessary duplication.
// This is copied verbatim from
// \Drupal\Core\Render\HtmlResponseAttachmentsProcessor::processAttachments.
try {
$response = $this
->renderPlaceholders($response);
} catch (EnforcedResponseException $e) {
return $e
->getResponse();
}
// Extract DFP slots; HtmlResponseAttachmentsProcessor does not
// know (nor need to know) how to process those.
$attachments = $response
->getAttachments();
if (isset($attachments['dfp_slot'])) {
$attachments['html_head'][] = [
$this
->getHeadTop(),
'dfp-js-head-top',
];
/** @var \Drupal\dfp\View\TagView $tag */
foreach ($attachments['dfp_slot'] as $tag_view) {
$attachments['html_head'][] = [
[
// Use a fake #type to prevent
// HtmlResponseAttachmentsProcessor::processHead() adding one.
'#type' => 'dfp_script',
'#theme' => 'dfp_slot_definition_js',
'#tag' => $tag_view,
],
'dfp-slot-definition-' . $tag_view
->id(),
];
}
$attachments['html_head'][] = [
$this
->getHeadBottom(),
'dfp-js-head-bottom',
];
unset($attachments['dfp_slot']);
}
$response
->setAttachments($attachments);
// Call HtmlResponseAttachmentsProcessor to process all other attachments.
return $this->htmlResponseAttachmentsProcessor
->processAttachments($response);
}