You are here

function asset_injector_page_attachments in Asset Injector 8

Same name and namespace in other branches
  1. 8.2 asset_injector.module \asset_injector_page_attachments()

Implements hook_page_attachments().

Give the render system the IDs of the curently active assets (that may depend on the curent page and other context - think config overrides). These IDs are mapped to the actual assets in Note that the IDs are namespaced with our module name.

Concerning cache contexts: The config override system may introduce additional cache contexts to aur assets. Think css that varies by domain. By adding our assets as cacheble dependencies all contexts they may carry apply to the rendered result.

Note that the list_cache_tags (library_info) are not added here and need not, as tha caller already does it. Setting asset entityies' list_cache_tags to library_info makes the library-info invalidate on asset change. While changing and deleting of assets will trigger invalidation by their individual cache tags, the list cache tags guarantees invalidation on new asset creation.

See also

asset_injector_library_info_build().

File

./asset_injector.module, line 109
Contains module asset_injector.

Code

function asset_injector_page_attachments(array &$attachments) {

  /** @var RendererInterface $renderer */
  $renderer = \Drupal::service('renderer');
  foreach (asset_injector_get_assets() as $asset) {
    if ($asset
      ->isActive()) {
      $attachments['#attached']['library'][] = 'asset_injector/' . $asset
        ->libraryNameSuffix();
      $renderer
        ->addCacheableDependency($attachments, $asset);
    }
  }
}