Mercurial > hg > cmmr2012-drupal-site
diff core/lib/Drupal/Core/Ajax/AnnounceCommand.php @ 5:12f9dff5fda9 tip
Update to Drupal core 8.7.1
author | Chris Cannam |
---|---|
date | Thu, 09 May 2019 15:34:47 +0100 |
parents | |
children |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/core/lib/Drupal/Core/Ajax/AnnounceCommand.php Thu May 09 15:34:47 2019 +0100 @@ -0,0 +1,81 @@ +<?php + +namespace Drupal\Core\Ajax; + +use Drupal\Core\Asset\AttachedAssets; + +/** + * AJAX command for a JavaScript Drupal.announce() call. + * + * @ingroup ajax + */ +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; + } + +}