diff --git a/core/lib/Drupal/Core/Ajax/AnnounceCommand.php b/core/lib/Drupal/Core/Ajax/AnnounceCommand.php index e35a835652..2dfd91c041 100644 --- a/core/lib/Drupal/Core/Ajax/AnnounceCommand.php +++ b/core/lib/Drupal/Core/Ajax/AnnounceCommand.php @@ -11,6 +11,16 @@ */ class AnnounceCommand implements CommandInterface, CommandWithAttachedAssetsInterface { + /** + * The assertive priority attribute value. + */ + const PRIORITY_ASSERTIVE = 'assertive'; + + /** + * The polite priority attribute value. + */ + const PRIORITY_POLITE = 'polite'; + /** * The text to be announced. * @@ -18,14 +28,24 @@ class AnnounceCommand implements CommandInterface, CommandWithAttachedAssetsInte */ protected $text; + /** + * The priority that will be used for the announcement. + * + * @var string + */ + protected $priority; + /** * Constructs an AnnounceCommand object. * * @param string $text * The text to be announced. + * @param string $priority + * The priority that will be used for the announcement. */ - public function __construct($text) { + public function __construct($text, $priority = AnnounceCommand::PRIORITY_POLITE) { $this->text = $text; + $this->priority = $priority; } /** @@ -35,6 +55,7 @@ public function render() { return [ 'command' => 'announce', 'text' => $this->text, + 'priority' => $this->priority, ]; } diff --git a/core/misc/ajax.es6.js b/core/misc/ajax.es6.js index 684902917d..bac72c6361 100644 --- a/core/misc/ajax.es6.js +++ b/core/misc/ajax.es6.js @@ -1344,11 +1344,13 @@ * The JSON response from the Ajax request. * @param {string} response.text * The text that will be read. + * @param {string} response.priority + * The priority that will be used for the announcement. * @param {number} [status] * The XMLHttpRequest status. */ announce(ajax, response, status) { - Drupal.announce(response.text); + Drupal.announce(response.text, response.priority); }, /** diff --git a/core/misc/ajax.js b/core/misc/ajax.js index 84ed72a15a..72af4ddf63 100644 --- a/core/misc/ajax.js +++ b/core/misc/ajax.js @@ -577,7 +577,7 @@ function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr window.alert(response.text, response.title); }, announce: function announce(ajax, response, status) { - Drupal.announce(response.text); + Drupal.announce(response.text, response.priority); }, 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 92300fe0c4..c1a5951e4c 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 @@ -46,12 +46,51 @@ function ajax_forms_test_advanced_commands_alert_callback($form, FormStateInterf /** - * Ajax form callback: Selects 'announce'. + * Ajax form callback: Selects 'announce' with no priority specified. */ function ajax_forms_test_advanced_commands_announce_callback($form, FormStateInterface $form_state) { + return _ajax_forms_test_create_announce_response('Default announcement.'); +} + +/** + * Ajax form callback: Selects 'announce' with 'polite' priority. + */ +function ajax_forms_test_advanced_commands_announce_polite_callback($form, FormStateInterface $form_state) { + return _ajax_forms_test_create_announce_response('Polite announcement.', 'polite'); +} + +/** + * Ajax form callback: Selects 'announce' with 'assertive' priority. + */ +function ajax_forms_test_advanced_commands_announce_assertive_callback($form, FormStateInterface $form_state) { + return _ajax_forms_test_create_announce_response('Assertive announcement.', 'assertive'); +} + +/** + * Ajax form callback: Selects 'announce' with two announce commands returned. + */ +function ajax_forms_test_advanced_commands_double_announce_callback($form, FormStateInterface $form_state) { + $response = _ajax_forms_test_create_announce_response('Assertive announcement.', 'assertive'); + return $response->addCommand(new AnnounceCommand('Another announcement.')); +} + +/** + * Create an AjaxResponse with an AnnounceCommand. + * + * @param string $announcement + * The announcement. + * @param string|null $priority + * (optional)The priority. + * + * @return \Drupal\Core\Ajax\AjaxResponse + * The AjaxResponse with an AnnounceCommand instance. + */ +function _ajax_forms_test_create_announce_response($announcement, $priority = NULL) { $response = new AjaxResponse(); - $response->addCommand(new AnnounceCommand('This is my announcement.')); - return $response; + if ($priority) { + return $response->addCommand(new AnnounceCommand($announcement, $priority)); + } + return $response->addCommand(new AnnounceCommand($announcement)); } /** 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 2fd9222878..8804b1bd37 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,7 +44,7 @@ public function buildForm(array $form, FormStateInterface $form_state) { ], ]; - // Shows the 'announce' command. + // Shows the 'announce' command with default priority. $form['announce_command_example'] = [ '#value' => $this->t("AJAX 'Announce': Click to announce"), '#type' => 'submit', @@ -53,6 +53,33 @@ public function buildForm(array $form, FormStateInterface $form_state) { ], ]; + // Shows the 'announce' command with 'polite' priority. + $form['announce_command_polite_example'] = [ + '#value' => $this->t("AJAX 'Announce': Click to announce with 'polite' priority"), + '#type' => 'submit', + '#ajax' => [ + 'callback' => 'ajax_forms_test_advanced_commands_announce_polite_callback', + ], + ]; + + // Shows the 'announce' command with 'assertive' priority. + $form['announce_command_assertive_example'] = [ + '#value' => $this->t("AJAX 'Announce': Click to announce with 'assertive' priority"), + '#type' => 'submit', + '#ajax' => [ + 'callback' => 'ajax_forms_test_advanced_commands_announce_assertive_callback', + ], + ]; + + // Shows the 'announce' command used twice in one AjaxResponse. + $form['announce_command_double_example'] = [ + '#value' => $this->t("AJAX 'Announce': Click to announce twice"), + '#type' => 'submit', + '#ajax' => [ + 'callback' => 'ajax_forms_test_advanced_commands_double_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 eb2ea429dd..0e2c15d32d 100644 --- a/core/tests/Drupal/FunctionalJavascriptTests/Ajax/CommandsTest.php +++ b/core/tests/Drupal/FunctionalJavascriptTests/Ajax/CommandsTest.php @@ -48,8 +48,19 @@ public function testAjaxCommands() { $this->drupalGet($form_path); $page->pressButton("AJAX 'Announce': Click to announce"); + $this->assertWaitPageContains('
Default announcement.
'); - $this->assertWaitPageContains('
This is my announcement.
'); + $this->drupalGet($form_path); + $page->pressButton("AJAX 'Announce': Click to announce with 'polite' priority"); + $this->assertWaitPageContains('
Polite announcement.
'); + + $this->drupalGet($form_path); + $page->pressButton("AJAX 'Announce': Click to announce with 'assertive' priority"); + $this->assertWaitPageContains('
Assertive announcement.
'); + + $this->drupalGet($form_path); + $page->pressButton("AJAX 'Announce': Click to announce twice"); + $this->assertWaitPageContains('
Assertive announcement.' . "\nAnother announcement.
"); // Tests the 'append' command. $page->pressButton("AJAX 'Append': Click to append something");