You are here

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

Same name and namespace in other branches
  1. 8 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:

  • styles
  • scripts
  • scripts_bottom

Overrides HtmlResponseAttachmentsProcessor::processAssetLibraries

File

src/Render/AmpHtmlResponseAttachmentsProcessor.php, line 85

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()) {

    // 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));
    }

    // After css has been rendered, strip non-AMP libraries before rendering
    // the javascript.
    // @TODO This can be optional if the theme provides granular control
    // over libraries using libraries-override, and if all javascript on the
    // site came in through the libraries system.
    foreach ($assets->libraries as $delta => $library) {

      // Rather than limit the libraries to ones provided by the AMP module,
      // limit them based on an /amp. prefix, i.e. amp/amp.image. This way
      // other modules could provide libraries that won't get stripped out.
      if (strpos($library, '/amp.') === FALSE && $library != 'amp/runtime') {
        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 {
    $variables = parent::processAssetLibraries($assets, $placeholders);
  }
  return $variables;
}