555a55156978f29266ce7e682b95c1648e4de367
[yaffs-website] / web / modules / contrib / advagg / src / Ajax / AjaxResponseAttachmentsProcessor.php
1 <?php
2
3 namespace Drupal\advagg\Ajax;
4
5 use Drupal\Core\Ajax\AjaxResponse;
6 use Drupal\Core\Ajax\AjaxResponseAttachmentsProcessor as CoreAjaxResponseAttachmentsProcessor;
7 use Drupal\Core\Ajax\AddCssCommand;
8 use Drupal\Core\Ajax\AppendCommand;
9 use Drupal\Core\Ajax\PrependCommand;
10 use Drupal\Core\Ajax\SettingsCommand;
11 use Drupal\Core\Asset\AttachedAssets;
12 use Drupal\Core\Render\AttachmentsResponseProcessorInterface;
13 use Symfony\Component\HttpFoundation\Request;
14
15 /**
16  * {@inheritdoc}
17  */
18 class AjaxResponseAttachmentsProcessor extends CoreAjaxResponseAttachmentsProcessor implements AttachmentsResponseProcessorInterface {
19
20   /**
21    * {@inheritdoc}
22    */
23   protected function buildAttachmentsCommands(AjaxResponse $response, Request $request) {
24     $ajax_page_state = $request->request->get('ajax_page_state') ?: $request->query->get('ajax_page_state');
25
26     // Aggregate CSS/JS if necessary, but only during normal site operation.
27     $optimize_css = !defined('MAINTENANCE_MODE') && $this->config->get('css.preprocess');
28     $optimize_js = !defined('MAINTENANCE_MODE') && $this->config->get('js.preprocess');
29
30     $attachments = $response->getAttachments();
31
32     // Resolve the attached libraries into asset collections.
33     $assets = new AttachedAssets();
34     $assets->setLibraries(isset($attachments['library']) ? $attachments['library'] : [])
35       ->setAlreadyLoadedLibraries(isset($ajax_page_state['libraries']) ? explode(',', $ajax_page_state['libraries']) : [])
36       ->setSettings(isset($attachments['drupalSettings']) ? $attachments['drupalSettings'] : []);
37     $css_assets = $this->assetResolver->getCssAssets($assets, $optimize_css);
38     list($js_assets_header, $js_assets_footer) = $this->assetResolver->getJsAssets($assets, $optimize_js);
39
40     // First, AttachedAssets::setLibraries() ensures duplicate libraries are
41     // removed: it converts it to a set of libraries if necessary. Second,
42     // AssetResolver::getJsSettings() ensures $assets contains the final set of
43     // JavaScript settings. AttachmentsResponseProcessorInterface also mandates
44     // that the response it processes contains the final attachment values, so
45     // update both the 'library' and 'drupalSettings' attachments accordingly.
46     $attachments['library'] = $assets->getLibraries();
47     $attachments['drupalSettings'] = $assets->getSettings();
48     $response->setAttachments($attachments);
49     $alter_type = 'ajax';
50
51     // Render the HTML to load these files, and add AJAX commands to insert this
52     // HTML in the page. Settings are handled separately, afterwards.
53     $settings = [];
54     if (isset($js_assets_header['drupalSettings'])) {
55       $settings = $js_assets_header['drupalSettings']['data'];
56       unset($js_assets_header['drupalSettings']);
57     }
58     if (isset($js_assets_footer['drupalSettings'])) {
59       $settings = $js_assets_footer['drupalSettings']['data'];
60       unset($js_assets_footer['drupalSettings']);
61     }
62
63     // Prepend commands to add the assets, preserving their relative order.
64     $resource_commands = [];
65     $prefetch = [];
66     if ($css_assets) {
67       $css_render_array = $this->cssCollectionRenderer->render($css_assets);
68       if (isset($css_render_array['prefetch'])) {
69         $prefetch = array_merge($prefetch, $css_render_array['prefetch']);
70         unset($css_render_array['prefetch']);
71       }
72
73       // Allow other modules to alter the assets before rendering.
74       // Call hook_advagg_asset_render_alter().
75       $asset_type = 'css';
76       $this->moduleHandler->alter('advagg_asset_render', $css_render_array, $alter_type, $asset_type);
77       $resource_commands[] = new AddCssCommand($this->renderer->renderPlain($css_render_array));
78     }
79     if ($js_assets_header) {
80       $js_header_render_array = $this->jsCollectionRenderer->render($js_assets_header);
81       if (isset($js_header_render_array['prefetch'])) {
82         $prefetch = array_merge($prefetch, $js_header_render_array['prefetch']);
83         unset($js_header_render_array['prefetch']);
84       }
85
86       // Allow other modules to alter the assets before rendering.
87       // Call hook_advagg_asset_render_alter().
88       $asset_type = 'js_header';
89       $this->moduleHandler->alter('advagg_asset_render', $js_header_render_array, $alter_type, $asset_type);
90       $resource_commands[] = new PrependCommand('head', $this->renderer->renderPlain($js_header_render_array));
91     }
92     if ($js_assets_footer) {
93       $js_footer_render_array = $this->jsCollectionRenderer->render($js_assets_footer);
94       if (isset($js_footer_render_array['prefetch'])) {
95         $prefetch = array_merge($prefetch, $js_footer_render_array['prefetch']);
96         unset($js_footer_render_array['prefetch']);
97       }
98
99       // Allow other modules to alter the assets before rendering.
100       // Call hook_advagg_asset_render_alter().
101       $asset_type = 'js_footer';
102       $this->moduleHandler->alter('advagg_asset_render', $js_footer_render_array, $alter_type, $asset_type);
103       $resource_commands[] = new AppendCommand('body', $this->renderer->renderPlain($js_footer_render_array));
104     }
105     if ($prefetch) {
106       $prefetch = array_unique($prefetch, SORT_REGULAR);
107
108       // Allow other modules to alter the assets before rendering.
109       // Call hook_advagg_asset_render_alter().
110       $asset_type = 'prefetch';
111       $this->moduleHandler->alter('advagg_asset_render', $prefetch, $alter_type, $asset_type);
112       $resource_commands[] = new PrependCommand('head', $this->renderer->renderPlain($prefetch));
113     }
114     foreach (array_reverse($resource_commands) as $resource_command) {
115       $response->addCommand($resource_command, TRUE);
116     }
117
118     // Prepend a command to merge changes and additions to drupalSettings.
119     if (!empty($settings)) {
120       // During Ajax requests basic path-specific settings are excluded from
121       // new drupalSettings values. The original page where this request comes
122       // from already has the right values. An Ajax request would update them
123       // with values for the Ajax request and incorrectly override the page's
124       // values.
125       // @see system_js_settings_alter()
126       unset($settings['path']);
127       $response->addCommand(new SettingsCommand($settings, TRUE), TRUE);
128     }
129
130     $commands = $response->getCommands();
131     $this->moduleHandler->alter('ajax_render', $commands);
132
133     return $commands;
134   }
135
136 }