You are here

private function HtmlResponseAttachmentsProcessor::generateAssetPlaceholder in Cookie Content Blocker 8

Generate a placeholder script referencing to the original asset.

Parameters

array $asset: The asset to create a placeholder for.

Return value

array The placeholder render array.

1 call to HtmlResponseAttachmentsProcessor::generateAssetPlaceholder()
HtmlResponseAttachmentsProcessor::renderAssetCollection in src/Render/HtmlResponseAttachmentsProcessor.php
Renders asset collections and inserts placeholders for blocked assets.

File

src/Render/HtmlResponseAttachmentsProcessor.php, line 160

Class

HtmlResponseAttachmentsProcessor
Processes attachments of HTML responses.

Namespace

Drupal\cookie_content_blocker\Render

Code

private function generateAssetPlaceholder(array $asset) : array {
  $id = Html::getUniqueId(Crypt::randomBytesBase64());
  $placeholder = [
    '#type' => 'html_tag',
    '#tag' => 'script',
    '#value' => '',
    '#attributes' => [
      'data-cookie-content-blocker-asset-id' => $id,
    ],
  ];
  $attached = [
    '#attached' => [
      'drupalSettings' => [
        'cookieContentBlocker' => [
          'blockedAssets' => [
            $id => (string) $this->renderer
              ->renderPlain($asset),
          ],
        ],
      ],
    ],
  ];

  // Merge attached settings back into the original assets.
  $placeholder_asset = AttachedAssets::createFromRenderArray($attached);
  $this->allowedAssets
    ->setSettings(NestedArray::mergeDeepArray([
    $placeholder_asset
      ->getSettings(),
    $this->allowedAssets
      ->getSettings(),
  ], TRUE));
  $this
    ->mergeSettings([
    'data' => $this->allowedAssets
      ->getSettings(),
  ]);
  return $placeholder;
}