diff --git a/core/lib/Drupal/Core/Ajax/CloseDialogCommand.php b/core/lib/Drupal/Core/Ajax/CloseDialogCommand.php new file mode 100644 index 0000000..a237239 --- /dev/null +++ b/core/lib/Drupal/Core/Ajax/CloseDialogCommand.php @@ -0,0 +1,24 @@ + 'closeDialog', + ); + } + +} diff --git a/core/lib/Drupal/Core/Ajax/OpenDialogCommand.php b/core/lib/Drupal/Core/Ajax/OpenDialogCommand.php new file mode 100644 index 0000000..5def98e --- /dev/null +++ b/core/lib/Drupal/Core/Ajax/OpenDialogCommand.php @@ -0,0 +1,54 @@ +title = $title; + $this->html = $html; + $this->settings = $settings; + } + + /** + * Implements Drupal\Core\Ajax\CommandInterface:render(). + */ + public function render() { + // @todo Should it be able to control the dialog settings? + return array( + 'command' => 'insert', + 'method' => NULL, + 'title' => $this->title, + 'selector' => '#drupal-modal', + 'data' => $this->html, + 'settings' => $this->settings, + ); + } + +} diff --git a/core/misc/dialog.ajax.js b/core/misc/dialog.ajax.js new file mode 100644 index 0000000..95e51ec --- /dev/null +++ b/core/misc/dialog.ajax.js @@ -0,0 +1,42 @@ +/** + * @file + * + * Integrates the dialog API with the drupal AJAX commands. + */ + +(function ($, Drupal, drupalSettings) { + +"use strict"; + + /** + * Command to close a modal dialog. + */ + Drupal.ajax.prototype.commands.closeDialog = function (ajax, response, status) { + var $modal = $("#drupal-modal"); + if ($modal.length) { + $modal.dialog.close(1); + } + }; + + /** + * Command to open a modal dialog. + */ + Drupal.ajax.prototype.commands.openDialog = function (ajax, response, status) { + var $modal = $('#drupal-modal'); + // Apply ajax settings. + $($modal).find('.ajax').once('ajax', function () { + var element_settings = {}; + element_settings.progress = { 'type': null }; + element_settings.dialog = { + modal: true + }; + + if ($(this).attr('href')) { + element_settings.url = $(this).attr('href'); + } + var base = $(this).attr('id'); + Drupal.ajax[base] = new Drupal.ajax(base, this, element_settings); + }); + }; + +})(jQuery, Drupal, drupalSettings); diff --git a/core/modules/system/system.module b/core/modules/system/system.module index eaaebc2..61b70ea 100644 --- a/core/modules/system/system.module +++ b/core/modules/system/system.module @@ -1148,6 +1148,22 @@ function system_library_info() { ), ); + // Drupal's dialog ajax integration. + $libraries['drupal.dialog.ajax'] = array( + 'title' => 'Drupal Dialog AJAX', + 'version' => VERSION, + 'js' => array( + 'core/misc/dialog.ajax.js' => array('group' => JS_LIBRARY, 'weight' => 3), + ), + 'dependencies' => array( + array('system', 'jquery'), + array('system', 'drupal'), + array('system', 'drupalSettings'), + array('system', 'drupal.ajax'), + array('system', 'drupal.dialog'), + ), + ); + // Drupal's states library. $libraries['drupal.states'] = array( 'title' => 'Drupal states', 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 efb1966..d59ab54 100644 --- a/core/modules/system/tests/modules/ajax_test/ajax_test.module +++ b/core/modules/system/tests/modules/ajax_test/ajax_test.module @@ -1,5 +1,8 @@ 'ajax_test_dialog_contents', 'access callback' => TRUE, ); + $items['ajax-test/dialog-commands'] = array( + 'title' => 'AJAX Dialog commands', + 'page callback' => 'ajax_test_dialog_commands', + 'access callback' => TRUE, + ); + $items['ajax-test/dialog-commands-close'] = array( + 'title' => 'AJAX Dialog commands: close', + 'page callback' => 'ajax_test_dialog_commands_close', + 'access callback' => TRUE, + ); return $items; } @@ -95,6 +108,7 @@ function ajax_test_dialog() { // Dialog behavior applied to a button. $build['form'] = drupal_get_form('ajax_test_dialog_form'); + $build['form']['#attached']['library'][] = array('system', 'drupal.dialog.ajax'); // Dialog behavior applied to a #type => 'link'. $build['link'] = array( @@ -125,6 +139,13 @@ function ajax_test_dialog() { 'wrapper' => 'ajax-test-dialog-wrapper-2', ), ), + 'link4' => array( + 'title' => 'Link 4 (modal with ajax commands)', + 'href' => 'ajax-test/dialog-commands', + 'ajax' => array( + 'dialog' => array('modal' => TRUE), + ), + ), ), ); return $build; @@ -181,3 +202,27 @@ function ajax_test_dialog_contents() { ); } +/** + * Menu callback: Returns ajax commands to open a dialog. + */ +function ajax_test_dialog_commands() { + $response = new AjaxResponse(); + + $content = array( + 'content' => array( + '#markup' => 'Example message', + ), + 'cancel'=> array( + '#type' => 'link', + '#title' => 'Cancel', + '#href' => 'ajax-test/dialog-commands-close', + '#ajax' => array(), + ), + ); + + $content = drupal_render($content); + $response->addCommand(new OpenDialogCommand(t('Title'), $content)); + + return $response; +} +