Skip to content
Snippets Groups Projects
Commit 7ed96147 authored by catch's avatar catch
Browse files

Issue #1691394 by pwolanin, nod_, tim.plunkett: Fixed Drupal settings gets broken by AJAX requests.

parent 75644c15
Branches
Tags
2 merge requests!7452Issue #1797438. HTML5 validation is preventing form submit and not fully...,!789Issue #3210310: Adjust Database API to remove deprecated Drupal 9 code in Drupal 10
......@@ -133,6 +133,15 @@ protected function ajaxRender(Request $request) {
$scripts = drupal_add_js();
if (!empty($scripts['settings'])) {
$settings = drupal_merge_js_settings($scripts['settings']['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.
// @see drupal_add_js
foreach (array('basePath', 'currentPath', 'scriptPath', 'pathPrefix') as $item) {
unset($settings[$item]);
}
$this->addCommand(new SettingsCommand($settings, TRUE), TRUE);
}
......
......@@ -29,7 +29,7 @@ public static function getInfo() {
/**
* Ensures ajax_render() returns JavaScript settings from the page request.
*/
function testAJAXRender() {
public function testAJAXRender() {
// Verify that settings command is generated when JavaScript settings are
// set via drupal_add_js().
$commands = $this->drupalGetAJAX('ajax-test/render');
......@@ -40,7 +40,7 @@ function testAJAXRender() {
/**
* Tests AjaxResponse::prepare() AJAX commands ordering.
*/
function testOrder() {
public function testOrder() {
$path = drupal_get_path('module', 'system');
$expected_commands = array();
......@@ -82,7 +82,7 @@ function testOrder() {
/**
* Tests behavior of ajax_render_error().
*/
function testAJAXRenderError() {
public function testAJAXRenderError() {
// Verify custom error message.
$edit = array(
'message' => 'Custom error message.',
......@@ -95,7 +95,7 @@ function testAJAXRenderError() {
/**
* Tests that new JavaScript and CSS files are lazy-loaded on an AJAX request.
*/
function testLazyLoad() {
public function testLazyLoad() {
$expected = array(
'setting_name' => 'ajax_forms_test_lazy_load_form_submit',
'setting_value' => 'executed',
......@@ -182,10 +182,22 @@ function testLazyLoad() {
$this->assertCommand(array_slice($commands, 2, 1), array('data' => $expected_js_html), format_string('Page now has the %js file.', array('%js' => $expected['js'])));
}
/**
* Tests that drupalSettings.currentPath is not updated on AJAX requests.
*/
public function testCurrentPathChange() {
$commands = $this->drupalPostAJAX('ajax_forms_test_lazy_load_form', array('add_files' => FALSE), array('op' => t('Submit')));
foreach ($commands as $command) {
if ($command['command'] == 'settings') {
$this->assertFalse(isset($command['settings']['currentPath']), 'Value of drupalSettings.currentPath is not updated after an AJAX request.');
}
}
}
/**
* Tests that overridden CSS files are not added during lazy load.
*/
function testLazyLoadOverriddenCSS() {
public function testLazyLoadOverriddenCSS() {
// The test theme overrides system.module.css without an implementation,
// thereby removing it.
theme_enable(array('test_theme'));
......
......@@ -534,6 +534,13 @@ function ajax_forms_test_validation_number_form_callback($form, $form_state) {
* Form builder: Builds a form that triggers a simple AJAX callback.
*/
function ajax_forms_test_lazy_load_form($form, &$form_state) {
// We attach a JavaScript setting, so that one of the generated AJAX commands
// will be a settings command. We can then check the settings command to
// ensure that the 'currentPath' setting is not part of the Ajax response.
$form['#attached']['js'][] = array(
'type' => 'setting',
'data' => array('test' => 'currentPathUpdate'),
);
$form['add_files'] = array(
'#type' => 'checkbox',
'#default_value' => FALSE,
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment