HtmlResponseAttachmentsProcessor.php in Advanced CSS/JS Aggregation 8.2
File
src/Render/HtmlResponseAttachmentsProcessor.php
View source
<?php
namespace Drupal\advagg\Render;
use Drupal\Core\Asset\AttachedAssetsInterface;
use Drupal\Core\Render\AttachmentsResponseProcessorInterface;
use Drupal\Core\Render\HtmlResponseAttachmentsProcessor as CoreHtmlResponseAttachmentsProcessor;
class HtmlResponseAttachmentsProcessor extends CoreHtmlResponseAttachmentsProcessor implements AttachmentsResponseProcessorInterface {
protected function processAssetLibraries(AttachedAssetsInterface $assets, array $placeholders) {
$variables = [
'prefetch' => [],
];
$render_type = 'html';
if (isset($placeholders['styles'])) {
$optimize_css = !defined('MAINTENANCE_MODE') && $this->config
->get('css.preprocess');
$variables['styles'] = $this->cssCollectionRenderer
->render($this->assetResolver
->getCssAssets($assets, $optimize_css));
$variables['prefetch'] = isset($variables['styles']['prefetch']) ? $variables['styles']['prefetch'] : [];
unset($variables['styles']['prefetch']);
$asset_type = 'styles';
$this->moduleHandler
->alter('advagg_asset_render', $variables['styles'], $render_type, $asset_type);
}
if (isset($placeholders['scripts']) || isset($placeholders['scripts_bottom'])) {
$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['prefetch'] += isset($variables['scripts']['prefetch']) ? $variables['scripts']['prefetch'] : [];
unset($variables['scripts']['prefetch']);
$asset_type = 'scripts';
$this->moduleHandler
->alter('advagg_asset_render', $variables['scripts'], $render_type, $asset_type);
$variables['scripts_bottom'] = $this->jsCollectionRenderer
->render($js_assets_footer);
$variables['prefetch'] += isset($variables['scripts_bottom']['prefetch']) ? $variables['scripts_bottom']['prefetch'] : [];
unset($variables['scripts_bottom']['prefetch']);
$asset_type = 'scripts_bottom';
$this->moduleHandler
->alter('advagg_asset_render', $variables['scripts_bottom'], $render_type, $asset_type);
}
$prefetch = array_unique($variables['prefetch'], SORT_REGULAR);
$asset_type = 'prefetch';
$this->moduleHandler
->alter('advagg_asset_render', $prefetch, $render_type, $asset_type);
if (isset($placeholders['styles'])) {
$variables['styles'] = array_merge($prefetch, $variables['styles']);
}
else {
$variables['scripts'] = array_merge($prefetch, $variables['scripts']);
}
unset($variables['prefetch']);
return $variables;
}
}