commit e6d6cb3d37c71fa216b1e5acf6862169f4461fa6 Author: Lee Rowlands Date: Wed Mar 27 18:19:48 2013 +1000 patch 34 diff --git a/core/lib/Drupal/Core/Ajax/DialogController.php b/core/lib/Drupal/Core/Ajax/DialogController.php index 5fe3c9a..1ff011e 100644 --- a/core/lib/Drupal/Core/Ajax/DialogController.php +++ b/core/lib/Drupal/Core/Ajax/DialogController.php @@ -118,6 +118,8 @@ public function dialog(Request $request, $_content, $modal = FALSE) { if (substr($target, 0, 1) != '#') { $target = '#' . $target; } + // This shouldn't be passed on to jQuery.ui.dialog. + unset($options['target']); } else { // Generate a target based on the controller. diff --git a/core/modules/config/config.admin.inc b/core/modules/config/config.admin.inc index 2c34832..4e12ea4 100644 --- a/core/modules/config/config.admin.inc +++ b/core/modules/config/config.admin.inc @@ -77,7 +77,10 @@ function config_admin_sync_form(array &$form, array &$form_state, StorageInterfa 'href' => 'admin/config/development/sync/diff/' . $config_file, 'attributes' => array( 'class' => array('use-ajax'), - 'data-accepts' => 'application/vnd.drupal-modal' + 'data-accepts' => 'application/vnd.drupal-modal', + 'data-dialog-options' => json_encode(array( + 'width' => 500 + )), ), ); $form[$config_change_type]['list']['#rows'][] = array( @@ -122,7 +125,7 @@ function config_admin_import_form_submit($form, &$form_state) { if (!lock()->lockMayBeAvailable(CONFIG_IMPORT_LOCK)) { drupal_set_message(t('Another request may be synchronizing configuration already.')); } - else if (config_import()) { + elseif (config_import()) { // Once a sync completes, we empty the staging directory. This prevents // changes from being accidentally overwritten by stray files getting // imported later. @@ -139,4 +142,3 @@ function config_admin_import_form_submit($form, &$form_state) { drupal_set_message(t('The import failed due to an error. Any errors have been logged.'), 'error'); } } - diff --git a/core/modules/config/lib/Drupal/config/Controller/ConfigController.php b/core/modules/config/lib/Drupal/config/Controller/ConfigController.php index 42bbdb6..471d656 100644 --- a/core/modules/config/lib/Drupal/config/Controller/ConfigController.php +++ b/core/modules/config/lib/Drupal/config/Controller/ConfigController.php @@ -80,6 +80,11 @@ public function diff($config_file) { $output['back'] = array( '#type' => 'link', + '#attributes' => array( + 'class' => array( + 'dialog-cancel', + ), + ), '#title' => "Back to 'Synchronize configuration' page.", '#href' => 'admin/config/development/sync', ); diff --git a/core/modules/simpletest/lib/Drupal/simpletest/WebTestBase.php b/core/modules/simpletest/lib/Drupal/simpletest/WebTestBase.php index 5cca38a..5b0e2fc 100644 --- a/core/modules/simpletest/lib/Drupal/simpletest/WebTestBase.php +++ b/core/modules/simpletest/lib/Drupal/simpletest/WebTestBase.php @@ -1161,6 +1161,16 @@ protected function drupalGet($path, array $options = array(), array $headers = a /** * Retrieve a Drupal path or an absolute path and JSON decode the result. + * + * @param string $path + * Path to request AJAX from. + * @param array $options + * Array of options to pass to url(). + * @param array $headers + * Array of headers. Eg array('Accept: application/vnd.drupal-ajax'). + * + * @return array + * Decoded json. */ protected function drupalGetAJAX($path, array $options = array(), array $headers = array()) { $headers[] = 'X-Requested-With: XMLHttpRequest'; diff --git a/core/modules/system/lib/Drupal/system/Tests/Ajax/DialogTest.php b/core/modules/system/lib/Drupal/system/Tests/Ajax/DialogTest.php index 32aea71..7b4e093 100644 --- a/core/modules/system/lib/Drupal/system/Tests/Ajax/DialogTest.php +++ b/core/modules/system/lib/Drupal/system/Tests/Ajax/DialogTest.php @@ -11,6 +11,10 @@ * Tests use of dialogs as wrappers for Ajax responses. */ class DialogTest extends AjaxTestBase { + + /** + * Declares test info. + */ public static function getInfo() { return array( 'name' => 'AJAX dialogs commands', @@ -22,7 +26,7 @@ public static function getInfo() { /** * Test sending non-JS and AJAX requests to open and manipulate modals. */ - function testDialog() { + public function testDialog() { // Ensure the elements render without notices or exceptions. $this->drupalGet('ajax-test/dialog'); @@ -35,8 +39,8 @@ function testDialog() { 'settings' => NULL, 'data' => $dialog_contents, 'dialogOptions' => array( - 'modal' => true, - 'title' => 'AJAX Dialog', + 'modal' => TRUE, + 'title' => 'AJAX Dialog contents', ), ); $normal_expected_response = array( @@ -45,8 +49,8 @@ function testDialog() { 'settings' => NULL, 'data' => $dialog_contents, 'dialogOptions' => array( - 'modal' => false, - 'title' => 'AJAX Dialog', + 'modal' => FALSE, + 'title' => 'AJAX Dialog contents', ), ); $close_expected_response = array( @@ -55,20 +59,29 @@ function testDialog() { ); // Check that requesting a modal dialog without JS goes to a page. - $this->drupalGet('ajax-test/dialog-contents/nojs/1'); + $this->drupalGet('ajax-test/dialog-contents'); $this->assertRaw($dialog_contents, 'Non-JS modal dialog page present.'); // Emulate going to the JS version of the page and check the JSON response. - $ajax_result = $this->drupalGetAJAX('ajax-test/dialog-contents/ajax/1'); + $ajax_result = $this->drupalGetAJAX('ajax-test/dialog-contents', array(), array('Accept: application/vnd.drupal-modal')); $this->assertEqual($modal_expected_response, $ajax_result[1], 'Modal dialog JSON response matches.'); // Check that requesting a "normal" dialog without JS goes to a page. - $this->drupalGet('ajax-test/dialog-contents/nojs'); + $this->drupalGet('ajax-test/dialog-contents'); $this->assertRaw($dialog_contents, 'Non-JS normal dialog page present.'); // Emulate going to the JS version of the page and check the JSON response. - $ajax_result = $this->drupalGetAJAX('ajax-test/dialog-contents/ajax'); - $this->assertEqual($normal_expected_response, $ajax_result[1], 'Normal dialog JSON response matches.'); + // This needs to use WebTestBase::drupalPostAJAX() so that the correct + // dialog options are sent. + $ajax_result = $this->drupalPostAJAX('ajax-test/dialog', array( + // We have to mock a form element to make drupalPost submit from a link. + 'textfield' => 'test', + ), array(), 'ajax-test/dialog-contents', array(), array('Accept: application/vnd.drupal-dialog'), NULL, array( + 'submit' => array( + 'dialog_options[target]' => 'ajax-test-dialog-wrapper-1', + ) + )); + $this->assertEqual($normal_expected_response, $ajax_result[3], 'Normal dialog JSON response matches.'); // Emulate closing the dialog via an AJAX request. There is no non-JS // version of this test. diff --git a/core/modules/system/tests/modules/ajax_test/ajax_test.module b/core/modules/system/tests/modules/ajax_test/ajax_test.module index 1cb6bf2..5fccd44 100644 --- a/core/modules/system/tests/modules/ajax_test/ajax_test.module +++ b/core/modules/system/tests/modules/ajax_test/ajax_test.module @@ -120,8 +120,11 @@ function ajax_test_dialog() { $build['link'] = array( '#type' => 'link', '#title' => 'Link 1 (modal)', - '#href' => 'ajax-test/dialog-contents/nojs/1', - '#attributes' => array('class' => array('use-ajax')), + '#href' => 'ajax-test/dialog-contents', + '#attributes' => array( + 'class' => array('use-ajax'), + 'data-accepts' => 'application/vnd.drupal-modal', + ), ); // Dialog behavior applied to links rendered by theme_links(). @@ -130,18 +133,33 @@ function ajax_test_dialog() { '#links' => array( 'link2' => array( 'title' => 'Link 2 (modal)', - 'href' => 'ajax-test/dialog-contents/nojs/1', - 'attributes' => array('class' => array('use-ajax')), + 'href' => 'ajax-test/dialog-contents', + 'attributes' => array( + 'class' => array('use-ajax'), + 'data-accepts' => 'application/vnd.drupal-modal', + 'data-dialog-options' => json_encode(array( + 'width' => 400, + )) + ), ), 'link3' => array( 'title' => 'Link 3 (non-modal)', - 'href' => 'ajax-test/dialog-contents/nojs', - 'attributes' => array('class' => array('use-ajax')), + 'href' => 'ajax-test/dialog-contents', + 'attributes' => array( + 'class' => array('use-ajax'), + 'data-accepts' => 'application/vnd.drupal-dialog', + 'data-dialog-options' => json_encode(array( + 'target' => 'ajax-test-dialog-wrapper-1', + 'width' => 800, + )) + ), ), 'link4' => array( 'title' => 'Link 4 (close non-modal if open)', 'href' => 'ajax-test/dialog-close', - 'attributes' => array('class' => array('use-ajax')), + 'attributes' => array( + 'class' => array('use-ajax'), + ), ), ), ); @@ -152,6 +170,12 @@ function ajax_test_dialog() { * Form builder: Renders buttons with #ajax['dialog']. */ function ajax_test_dialog_form($form, &$form_state) { + // In order to use WebTestBase::drupalPostAJAX() to POST from a link, we need + // to have a dummy field we can set in WebTestBase::drupalPost() else it won't + // submit anything. + $form['textfield'] = array( + '#type' => 'hidden' + ); $form['button1'] = array( '#type' => 'submit', '#name' => 'button1', @@ -182,26 +206,47 @@ function ajax_test_dialog_form_submit($form, &$form_state) { * AJAX callback handler for ajax_test_dialog_form(). */ function ajax_test_dialog_form_callback_modal($form, &$form_state) { - return ajax_test_dialog_contents('ajax', TRUE); + return _ajax_test_dialog(TRUE); } /** * AJAX callback handler for ajax_test_dialog_form(). */ function ajax_test_dialog_form_callback_nonmodal($form, &$form_state) { - return ajax_test_dialog_contents('ajax', FALSE); + return _ajax_test_dialog(FALSE); +} + +/** + * Util to render dialog in ajax callback. + * + * @param bool $is_modal + * (optional) TRUE if modal, FALSE if plain dialog. Defaults to FALSE. + */ +function _ajax_test_dialog($is_modal = FALSE) { + $content = ajax_test_dialog_contents(); + $response = new AjaxResponse(); + $title = t('AJAX Dialog contents'); + $html = drupal_render($content); + if ($is_modal) { + $response->addCommand(new OpenModalDialogCommand($title, $html)); + } + else { + $selector = '#ajax-test-dialog-wrapper-1'; + $response->addCommand(new OpenDialogCommand($selector, $title, $html)); + } + return $response; } /** * Menu callback: Returns the contents for dialogs opened by ajax_test_dialog(). */ -function ajax_test_dialog_contents($page_mode = 'nojs', $is_modal = 0) { +function ajax_test_dialog_contents() { // This is a regular render array; the keys do not have special meaning. $content = array( 'content' => array( '#markup' => 'Example message', ), - 'cancel'=> array( + 'cancel' => array( '#type' => 'link', '#title' => 'Cancel', '#href' => '', @@ -213,22 +258,7 @@ function ajax_test_dialog_contents($page_mode = 'nojs', $is_modal = 0) { ), ); - if ($page_mode === 'ajax') { - $response = new AjaxResponse(); - $title = t('AJAX Dialog'); - $html = drupal_render($content); - if ($is_modal) { - $response->addCommand(new OpenModalDialogCommand($title, $html)); - } - else { - $selector = '#ajax-test-dialog-wrapper-1'; - $response->addCommand(new OpenDialogCommand($selector, $title, $html)); - } - return $response; - } - else { - return $content; - } + return $content; } /**