You are here

protected function AmpHtmlResponseAttachmentsProcessor::processAssetLibraries in Accelerated Mobile Pages (AMP) 8

Same name and namespace in other branches
  1. 8.3 src/Render/AmpHtmlResponseAttachmentsProcessor.php \Drupal\amp\Render\AmpHtmlResponseAttachmentsProcessor::processAssetLibraries()
  2. 8.2 src/Render/AmpHtmlResponseAttachmentsProcessor.php \Drupal\amp\Render\AmpHtmlResponseAttachmentsProcessor::processAssetLibraries()

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:

  • scripts
1 call to AmpHtmlResponseAttachmentsProcessor::processAssetLibraries()
AmpHtmlResponseAttachmentsProcessor::processAttachments in src/Render/AmpHtmlResponseAttachmentsProcessor.php
Processes the attachments of a response that has attachments.

File

src/Render/AmpHtmlResponseAttachmentsProcessor.php, line 144
Contains \Drupal\amp\Render\AmpHtmlResponseAttachmentsProcessor.

Class

AmpHtmlResponseAttachmentsProcessor
Processes attachments of AMP HTML responses.

Namespace

Drupal\amp\Render

Code

protected function processAssetLibraries(AttachedAssetsInterface $assets, array $placeholders) {
  $variables = [];
  if ($this->ampContext
    ->isAmpRoute()) {
    foreach ($assets->libraries as $delta => $library) {
      if (strpos($library, 'amp/') === FALSE) {
        unset($assets->libraries[$delta]);
      }
    }

    // Print amp scripts - if any are present.
    if (isset($placeholders['scripts']) || isset($placeholders['scripts_bottom'])) {

      // Do not optimize JS.
      $optimize_js = FALSE;
      list($js_assets_header, $js_assets_footer) = $this->assetResolver
        ->getJsAssets($assets, $optimize_js);
      $variables['scripts'] = $this->jsCollectionRenderer
        ->render($js_assets_header);
    }
  }
  else {

    // Print styles - if present.
    if (isset($placeholders['styles'])) {

      // Optimize CSS if necessary, but only during normal site operation.
      $optimize_css = !defined('MAINTENANCE_MODE') && $this->config
        ->get('css.preprocess');
      $variables['styles'] = $this->cssCollectionRenderer
        ->render($this->assetResolver
        ->getCssAssets($assets, $optimize_css));
    }

    // Print scripts - if any are present.
    if (isset($placeholders['scripts']) || isset($placeholders['scripts_bottom'])) {

      // Optimize JS if necessary, but only during normal site operation.
      $optimize_js = !defined('MAINTENANCE_MODE') && !\Drupal::state()
        ->get('system.maintenance_mode') && $this->config
        ->get('js.preprocess');
      list($js_assets_header, $js_assets_footer) = $this->assetResolver
        ->getJsAssets($assets, $optimize_js);
      $variables['scripts'] = $this->jsCollectionRenderer
        ->render($js_assets_header);
      $variables['scripts_bottom'] = $this->jsCollectionRenderer
        ->render($js_assets_footer);
    }
  }
  return $variables;
}