diff --git a/core/lib/Drupal/Core/Ajax/AnnounceCommand.php b/core/lib/Drupal/Core/Ajax/AnnounceCommand.php new file mode 100644 index 0000000000..3c612f77e9 --- /dev/null +++ b/core/lib/Drupal/Core/Ajax/AnnounceCommand.php @@ -0,0 +1,51 @@ +text = $text; + } + + /** + * {@inheritdoc} + */ + public function render() { + return [ + 'command' => 'announce', + 'text' => $this->text, + ]; + } + + /** + * {@inheritdoc} + */ + public function getAttachedAssets() { + $assets = new AttachedAssets(); + $assets->setLibraries(['core/drupal.announce']); + return $assets; + } + +} diff --git a/core/misc/ajax.es6.js b/core/misc/ajax.es6.js index 440e5906dc..684902917d 100644 --- a/core/misc/ajax.es6.js +++ b/core/misc/ajax.es6.js @@ -1335,6 +1335,22 @@ window.alert(response.text, response.title); }, + /** + * Command to provide triggers audio UAs to read the supplied text. + * + * @param {Drupal.Ajax} [ajax] + * {@link Drupal.Ajax} object created by {@link Drupal.ajax}. + * @param {object} response + * The JSON response from the Ajax request. + * @param {string} response.text + * The text that will be read. + * @param {number} [status] + * The XMLHttpRequest status. + */ + announce(ajax, response, status) { + Drupal.announce(response.text); + }, + /** * Command to set the window.location, redirecting the browser. * diff --git a/core/misc/ajax.js b/core/misc/ajax.js index 73b4dcca69..84ed72a15a 100644 --- a/core/misc/ajax.js +++ b/core/misc/ajax.js @@ -576,6 +576,9 @@ function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr alert: function alert(ajax, response, status) { window.alert(response.text, response.title); }, + announce: function announce(ajax, response, status) { + Drupal.announce(response.text); + }, redirect: function redirect(ajax, response, status) { window.location = response.url; }, diff --git a/core/modules/system/tests/modules/ajax_forms_test/ajax_forms_test.module b/core/modules/system/tests/modules/ajax_forms_test/ajax_forms_test.module index a93372d06d..92300fe0c4 100644 --- a/core/modules/system/tests/modules/ajax_forms_test/ajax_forms_test.module +++ b/core/modules/system/tests/modules/ajax_forms_test/ajax_forms_test.module @@ -9,6 +9,7 @@ use Drupal\Core\Ajax\AfterCommand; use Drupal\Core\Ajax\AjaxResponse; use Drupal\Core\Ajax\AlertCommand; +use Drupal\Core\Ajax\AnnounceCommand; use Drupal\Core\Ajax\AppendCommand; use Drupal\Core\Ajax\BeforeCommand; use Drupal\Core\Ajax\ChangedCommand; @@ -43,6 +44,16 @@ function ajax_forms_test_advanced_commands_alert_callback($form, FormStateInterf return $response; } + +/** + * Ajax form callback: Selects 'announce'. + */ +function ajax_forms_test_advanced_commands_announce_callback($form, FormStateInterface $form_state) { + $response = new AjaxResponse(); + $response->addCommand(new AnnounceCommand('This is my announcement.')); + return $response; +} + /** * Ajax form callback: Selects 'append'. */ diff --git a/core/modules/system/tests/modules/ajax_forms_test/src/Form/AjaxFormsTestCommandsForm.php b/core/modules/system/tests/modules/ajax_forms_test/src/Form/AjaxFormsTestCommandsForm.php index 924a9fda61..c79e29adfe 100644 --- a/core/modules/system/tests/modules/ajax_forms_test/src/Form/AjaxFormsTestCommandsForm.php +++ b/core/modules/system/tests/modules/ajax_forms_test/src/Form/AjaxFormsTestCommandsForm.php @@ -44,6 +44,15 @@ public function buildForm(array $form, FormStateInterface $form_state) { ], ]; + // Shows the 'alert' command. + $form['announce_command_example'] = [ + '#value' => $this->t("AJAX 'Announce': Click to announce"), + '#type' => 'submit', + '#ajax' => [ + 'callback' => 'ajax_forms_test_advanced_commands_announce_callback', + ], + ]; + // Shows the 'append' command. $form['append_command_example'] = [ '#value' => $this->t("AJAX 'Append': Click to append something"), diff --git a/core/tests/Drupal/FunctionalJavascriptTests/Ajax/CommandsTest.php b/core/tests/Drupal/FunctionalJavascriptTests/Ajax/CommandsTest.php index 0db5e5f74e..eb2ea429dd 100644 --- a/core/tests/Drupal/FunctionalJavascriptTests/Ajax/CommandsTest.php +++ b/core/tests/Drupal/FunctionalJavascriptTests/Ajax/CommandsTest.php @@ -46,6 +46,11 @@ public function testAjaxCommands() { $page->pressButton("AJAX 'Alert': Click to alert"); $this->assertWaitPageContains('
Alert
'); + $this->drupalGet($form_path); + $page->pressButton("AJAX 'Announce': Click to announce"); + + $this->assertWaitPageContains('
This is my announcement.
'); + // Tests the 'append' command. $page->pressButton("AJAX 'Append': Click to append something"); $this->assertWaitPageContains('
Append inside this divAppended text
'); diff --git a/core/tests/Drupal/Tests/Core/Ajax/AjaxCommandsTest.php b/core/tests/Drupal/Tests/Core/Ajax/AjaxCommandsTest.php index 1dd66b8328..b528659cde 100644 --- a/core/tests/Drupal/Tests/Core/Ajax/AjaxCommandsTest.php +++ b/core/tests/Drupal/Tests/Core/Ajax/AjaxCommandsTest.php @@ -2,6 +2,8 @@ namespace Drupal\Tests\Core\Ajax; +use Drupal\Core\Ajax\AnnounceCommand; +use Drupal\Core\Asset\AttachedAssets; use Drupal\Tests\UnitTestCase; use Drupal\Core\Ajax\AddCssCommand; use Drupal\Core\Ajax\AfterCommand; @@ -78,6 +80,23 @@ public function testAlertCommand() { $this->assertEquals($expected, $command->render()); } + /** + * @covers \Drupal\Core\Ajax\AnnounceCommand + */ + public function testAnnounceCommand() { + $command = new AnnounceCommand('Things are going to change!'); + $expected_assets = new AttachedAssets(); + $expected_assets->setLibraries(['core/drupal.announce']); + $this->assertEquals($expected_assets, $command->getAttachedAssets()); + + $expected = [ + 'command' => 'announce', + 'text' => 'Things are going to change!', + ]; + + $this->assertSame($expected, $command->render()); + } + /** * @covers \Drupal\Core\Ajax\AppendCommand */