View source
<?php
namespace Drupal\advagg\Ajax;
use Drupal\Core\Ajax\AjaxResponse;
use Drupal\Core\Ajax\AjaxResponseAttachmentsProcessor as CoreAjaxResponseAttachmentsProcessor;
use Drupal\Core\Ajax\AddCssCommand;
use Drupal\Core\Ajax\AppendCommand;
use Drupal\Core\Ajax\PrependCommand;
use Drupal\Core\Ajax\SettingsCommand;
use Drupal\Core\Asset\AttachedAssets;
use Drupal\Core\Render\AttachmentsResponseProcessorInterface;
use Symfony\Component\HttpFoundation\Request;
class AjaxResponseAttachmentsProcessor extends CoreAjaxResponseAttachmentsProcessor implements AttachmentsResponseProcessorInterface {
protected function buildAttachmentsCommands(AjaxResponse $response, Request $request) {
$ajax_page_state = $request->request
->get('ajax_page_state') ?: $request->query
->get('ajax_page_state');
$optimize_css = !defined('MAINTENANCE_MODE') && $this->config
->get('css.preprocess');
$optimize_js = !defined('MAINTENANCE_MODE') && $this->config
->get('js.preprocess');
$attachments = $response
->getAttachments();
$assets = new AttachedAssets();
$assets
->setLibraries(isset($attachments['library']) ? $attachments['library'] : [])
->setAlreadyLoadedLibraries(isset($ajax_page_state['libraries']) ? explode(',', $ajax_page_state['libraries']) : [])
->setSettings(isset($attachments['drupalSettings']) ? $attachments['drupalSettings'] : []);
$css_assets = $this->assetResolver
->getCssAssets($assets, $optimize_css);
list($js_assets_header, $js_assets_footer) = $this->assetResolver
->getJsAssets($assets, $optimize_js);
$attachments['library'] = $assets
->getLibraries();
$attachments['drupalSettings'] = $assets
->getSettings();
$response
->setAttachments($attachments);
$alter_type = 'ajax';
$settings = [];
if (isset($js_assets_header['drupalSettings'])) {
$settings = $js_assets_header['drupalSettings']['data'];
unset($js_assets_header['drupalSettings']);
}
if (isset($js_assets_footer['drupalSettings'])) {
$settings = $js_assets_footer['drupalSettings']['data'];
unset($js_assets_footer['drupalSettings']);
}
$resource_commands = [];
$prefetch = [];
if ($css_assets) {
$css_render_array = $this->cssCollectionRenderer
->render($css_assets);
if (isset($css_render_array['prefetch'])) {
$prefetch = array_merge($prefetch, $css_render_array['prefetch']);
unset($css_render_array['prefetch']);
}
$asset_type = 'css';
$this->moduleHandler
->alter('advagg_asset_render', $css_render_array, $alter_type, $asset_type);
$resource_commands[] = new AddCssCommand($this->renderer
->renderPlain($css_render_array));
}
if ($js_assets_header) {
$js_header_render_array = $this->jsCollectionRenderer
->render($js_assets_header);
if (isset($js_header_render_array['prefetch'])) {
$prefetch = array_merge($prefetch, $js_header_render_array['prefetch']);
unset($js_header_render_array['prefetch']);
}
$asset_type = 'js_header';
$this->moduleHandler
->alter('advagg_asset_render', $js_header_render_array, $alter_type, $asset_type);
$resource_commands[] = new PrependCommand('head', $this->renderer
->renderPlain($js_header_render_array));
}
if ($js_assets_footer) {
$js_footer_render_array = $this->jsCollectionRenderer
->render($js_assets_footer);
if (isset($js_footer_render_array['prefetch'])) {
$prefetch = array_merge($prefetch, $js_footer_render_array['prefetch']);
unset($js_footer_render_array['prefetch']);
}
$asset_type = 'js_footer';
$this->moduleHandler
->alter('advagg_asset_render', $js_footer_render_array, $alter_type, $asset_type);
$resource_commands[] = new AppendCommand('body', $this->renderer
->renderPlain($js_footer_render_array));
}
if ($prefetch) {
$prefetch = array_unique($prefetch, SORT_REGULAR);
$asset_type = 'prefetch';
$this->moduleHandler
->alter('advagg_asset_render', $prefetch, $alter_type, $asset_type);
$resource_commands[] = new PrependCommand('head', $this->renderer
->renderPlain($prefetch));
}
foreach (array_reverse($resource_commands) as $resource_command) {
$response
->addCommand($resource_command, TRUE);
}
if (!empty($settings)) {
unset($settings['path']);
$response
->addCommand(new SettingsCommand($settings, TRUE), TRUE);
}
$commands = $response
->getCommands();
$this->moduleHandler
->alter('ajax_render', $commands);
return $commands;
}
}