core/lib/Drupal/Core/Ajax/AjaxResponse.php | 18 ++++++++---------- .../system/src/Tests/Common/JavaScriptTest.php | 10 ++++++++-- .../tests/modules/common_test/common_test.module | 15 +++++++++++++++ core/modules/system/theme.api.php | 19 ++++++++++++++++++- 4 files changed, 49 insertions(+), 13 deletions(-) diff --git a/core/lib/Drupal/Core/Ajax/AjaxResponse.php b/core/lib/Drupal/Core/Ajax/AjaxResponse.php index 2ae1853..df8a776 100644 --- a/core/lib/Drupal/Core/Ajax/AjaxResponse.php +++ b/core/lib/Drupal/Core/Ajax/AjaxResponse.php @@ -129,8 +129,8 @@ protected function ajaxRender(Request $request) { // HTML in the page. We pass TRUE as the $skip_alter argument to prevent the // data from being altered again, as we already altered it above. Settings // are handled separately, afterwards. - if (isset($items['js']['settings'])) { - unset($items['js']['settings']); + if (isset($items['js']['drupalSettings'])) { + unset($items['js']['drupalSettings']); } $styles = drupal_get_css($items['css'], TRUE); $scripts_footer = drupal_get_js('footer', $items['js'], TRUE, TRUE); @@ -153,17 +153,15 @@ protected function ajaxRender(Request $request) { // Prepend a command to merge changes and additions to drupalSettings. $scripts = _drupal_add_js(); - if (!empty($scripts['settings'])) { - $settings = drupal_merge_js_settings($scripts['settings']['data']); + if (!empty($scripts['drupalSettings'])) { + $settings = drupal_merge_js_settings($scripts['drupalSettings']['data']); // During Ajax requests basic path-specific settings are excluded from // new drupalSettings values. The original page where this request comes - // from already has the right values for the keys below. An Ajax request - // would update them with values for the Ajax request and incorrectly - // override the page's values. + // from already has the right values. An Ajax request would update them + // with values for the Ajax request and incorrectly override the page's + // values. // @see _drupal_add_js() - foreach (array('basePath', 'currentPath', 'scriptPath', 'pathPrefix') as $item) { - unset($settings[$item]); - } + unset($settings['path']); $this->addCommand(new SettingsCommand($settings, TRUE), TRUE); } diff --git a/core/modules/system/src/Tests/Common/JavaScriptTest.php b/core/modules/system/src/Tests/Common/JavaScriptTest.php index a8a4874..e862496 100644 --- a/core/modules/system/src/Tests/Common/JavaScriptTest.php +++ b/core/modules/system/src/Tests/Common/JavaScriptTest.php @@ -77,11 +77,11 @@ function testAddSetting() { $attached['#attached']['library'][] = 'core/drupalSettings'; $this->render($attached); $javascript = _drupal_add_js(); - $last_settings = reset($javascript['settings']['data']); + $last_settings = reset($javascript['drupalSettings']['data']); $this->assertTrue(array_key_exists('currentPath', $last_settings['path']), 'The current path JavaScript setting is set correctly.'); $javascript = _drupal_add_js(array('drupal' => 'rocks', 'dries' => 280342800), 'setting'); - $last_settings = end($javascript['settings']['data']); + $last_settings = end($javascript['drupalSettings']['data']); $this->assertEqual(280342800, $last_settings['dries'], 'JavaScript setting is set correctly.'); $this->assertEqual('rocks', $last_settings['drupal'], 'The other JavaScript setting is set correctly.'); } @@ -241,6 +241,12 @@ function testHeaderSetting() { $settings_two['moduleName']['thingiesOnPage']['id1'] = array(); $this->assertIdentical($settings_one, $parsed_settings['commonTestRealWorldIdentical'], '_drupal_add_js handled real world test case 1 correctly.'); $this->assertEqual($settings_two, $parsed_settings['commonTestRealWorldAlmostIdentical'], '_drupal_add_js handled real world test case 2 correctly.'); + + // Tests whether altering JavaScript settings via hook_js_settings_alter() + // is working as expected. + // @see common_test_js_settings_alter() + $this->assertIdentical($parsed_settings['locale']['pluralDelimiter'], '☃'); + $this->assertIdentical($parsed_settings['foo'], 'bar'); } /** diff --git a/core/modules/system/tests/modules/common_test/common_test.module b/core/modules/system/tests/modules/common_test/common_test.module index 9cf802d..1148810 100644 --- a/core/modules/system/tests/modules/common_test/common_test.module +++ b/core/modules/system/tests/modules/common_test/common_test.module @@ -289,3 +289,18 @@ function common_test_page_attachments_alter(array &$page) { ]; } } + +/** + * Implements hook_js_settings_alter(). + * + * @see \Drupal\system\Tests\Common\JavaScriptTest::testHeaderSetting() + */ +function common_test_js_settings_alter(&$settings) { + // Modify an existing setting. + if (isset($settings['locale']['pluralDelimiter'])) { + $settings['locale']['pluralDelimiter'] = '☃'; + } + + // Add a setting. + $settings['foo'] = 'bar'; +} diff --git a/core/modules/system/theme.api.php b/core/modules/system/theme.api.php index 5747ff9..85a6224 100644 --- a/core/modules/system/theme.api.php +++ b/core/modules/system/theme.api.php @@ -717,8 +717,25 @@ function hook_js_alter(&$javascript) { $javascript['core/assets/vendor/jquery/jquery.js']['data'] = drupal_get_path('module', 'jquery_update') . '/jquery.js'; } +/** + * Perform necessary alterations to the JavaScript settings (drupalSettings). + * + * @param array &$settings + * An array of all JavaScript settings (drupalSettings) being presented on the + * page. + * + * @see _drupal_add_js() + * @see drupal_get_js() + * @see drupal_js_defaults() + */ function hook_js_settings_alter(array &$settings) { - // @todo document… + // Add settings. + $settings['user']['uid'] = \Drupal::currentUser(); + + // Manipulate settings. + if (isset($settings['dialog'])) { + $settings['dialog']['autoResize'] = FALSE; + } } /**