class AnnounceCommand in Drupal 9
Same name and namespace in other branches
- 8 core/lib/Drupal/Core/Ajax/AnnounceCommand.php \Drupal\Core\Ajax\AnnounceCommand
- 10 core/lib/Drupal/Core/Ajax/AnnounceCommand.php \Drupal\Core\Ajax\AnnounceCommand
AJAX command for a JavaScript Drupal.announce() call.
Developers should be extra careful if this command and \Drupal\Core\Ajax\MessageCommand are included in the same response. By default, MessageCommand will also call Drupal.announce() and announce the message to the screen reader (unless the option to suppress announcements is passed to the constructor). Manual testing with a screen reader is strongly recommended.
Hierarchy
- class \Drupal\Core\Ajax\AnnounceCommand implements CommandInterface, CommandWithAttachedAssetsInterface
Expanded class hierarchy of AnnounceCommand
See also
\Drupal\Core\Ajax\MessageCommand
Related topics
3 files declare their use of AnnounceCommand
- AjaxCommandsTest.php in core/
tests/ Drupal/ Tests/ Core/ Ajax/ AjaxCommandsTest.php - ajax_forms_test.module in core/
modules/ system/ tests/ modules/ ajax_forms_test/ ajax_forms_test.module - Simpletest mock module for Ajax forms testing.
- MediaLibraryWidget.php in core/
modules/ media_library/ src/ Plugin/ Field/ FieldWidget/ MediaLibraryWidget.php
File
- core/
lib/ Drupal/ Core/ Ajax/ AnnounceCommand.php, line 21
Namespace
Drupal\Core\AjaxView source
class AnnounceCommand implements CommandInterface, CommandWithAttachedAssetsInterface {
/**
* The assertive priority attribute value.
*
* @var string
*/
const PRIORITY_ASSERTIVE = 'assertive';
/**
* The polite priority attribute value.
*
* @var string
*/
const PRIORITY_POLITE = 'polite';
/**
* The text to be announced.
*
* @var string
*/
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|null $priority
* (optional) The priority that will be used for the announcement. Defaults
* to NULL which will not set a 'priority' in the response sent to the
* client and therefore the JavaScript Drupal.announce() default of 'polite'
* will be used for the message.
*/
public function __construct($text, $priority = NULL) {
$this->text = $text;
$this->priority = $priority;
}
/**
* {@inheritdoc}
*/
public function render() {
$render = [
'command' => 'announce',
'text' => $this->text,
];
if ($this->priority !== NULL) {
$render['priority'] = $this->priority;
}
return $render;
}
/**
* {@inheritdoc}
*/
public function getAttachedAssets() {
$assets = new AttachedAssets();
$assets
->setLibraries([
'core/drupal.announce',
]);
return $assets;
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
AnnounceCommand:: |
protected | property | The priority that will be used for the announcement. | |
AnnounceCommand:: |
protected | property | The text to be announced. | |
AnnounceCommand:: |
public | function |
Gets the attached assets. Overrides CommandWithAttachedAssetsInterface:: |
|
AnnounceCommand:: |
constant | The assertive priority attribute value. | ||
AnnounceCommand:: |
constant | The polite priority attribute value. | ||
AnnounceCommand:: |
public | function |
Return an array to be run through json_encode and sent to the client. Overrides CommandInterface:: |
|
AnnounceCommand:: |
public | function | Constructs an AnnounceCommand object. |