You are here

protected function HtmlResponseAttachmentsProcessor::processAssetLibraries in Cookie Content Blocker 8

Processes asset libraries into render arrays.

Parameters

\Drupal\Core\Asset\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

Overrides HtmlResponseAttachmentsProcessor::processAssetLibraries

File

src/Render/HtmlResponseAttachmentsProcessor.php, line 114

Class

HtmlResponseAttachmentsProcessor
Processes attachments of HTML responses.

Namespace

Drupal\cookie_content_blocker\Render

Code

protected function processAssetLibraries(AttachedAssetsInterface $assets, array $placeholders) : array {
  if (!$this->libraryManager
    ->hasBlockedLibraries()) {
    return parent::processAssetLibraries($assets, $placeholders);
  }
  $variables = [
    'styles' => [],
    'scripts' => [],
    'scripts_bottom' => [],
  ];
  $blocked_libraries = $this->libraryManager
    ->getBlockedLibraries();
  $allowed_libraries = array_filter($assets
    ->getLibraries(), function ($library) use ($blocked_libraries) {
    return !in_array($library, $blocked_libraries, TRUE);
  });
  $this->allowedAssets = $assets
    ->setLibraries($allowed_libraries);
  $this->blockedAssets = AttachedAssets::createFromRenderArray([
    '#attached' => [
      'library' => $blocked_libraries,
    ],
  ]);
  $this->blockedAssets
    ->setAlreadyLoadedLibraries($allowed_libraries);
  foreach (array_keys($variables) as $type) {
    $process_method = 'process' . Container::camelize($type);
    if (!isset($placeholders[$type]) || !is_callable([
      $this,
      $process_method,
    ])) {
      continue;
    }
    $variables[$type] = $this
      ->{$process_method}();
  }

  // After everything is processed we restore the drupal settings on its
  // original position, now containing our extra settings created by
  // placeholders as well.
  $settings = self::$drupalSettings;
  array_unshift($variables[$settings['position']], ...$this
    ->renderAssetCollection([
    $settings,
  ], $this->jsCollectionRenderer));
  return $variables;
}