annotate 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
rev   line source
Chris@5 1 <?php
Chris@5 2
Chris@5 3 namespace Drupal\Core\Ajax;
Chris@5 4
Chris@5 5 use Drupal\Core\Asset\AttachedAssets;
Chris@5 6
Chris@5 7 /**
Chris@5 8 * AJAX command for a JavaScript Drupal.announce() call.
Chris@5 9 *
Chris@5 10 * @ingroup ajax
Chris@5 11 */
Chris@5 12 class AnnounceCommand implements CommandInterface, CommandWithAttachedAssetsInterface {
Chris@5 13
Chris@5 14 /**
Chris@5 15 * The assertive priority attribute value.
Chris@5 16 *
Chris@5 17 * @var string
Chris@5 18 */
Chris@5 19 const PRIORITY_ASSERTIVE = 'assertive';
Chris@5 20
Chris@5 21 /**
Chris@5 22 * The polite priority attribute value.
Chris@5 23 *
Chris@5 24 * @var string
Chris@5 25 */
Chris@5 26 const PRIORITY_POLITE = 'polite';
Chris@5 27
Chris@5 28 /**
Chris@5 29 * The text to be announced.
Chris@5 30 *
Chris@5 31 * @var string
Chris@5 32 */
Chris@5 33 protected $text;
Chris@5 34
Chris@5 35 /**
Chris@5 36 * The priority that will be used for the announcement.
Chris@5 37 *
Chris@5 38 * @var string
Chris@5 39 */
Chris@5 40 protected $priority;
Chris@5 41
Chris@5 42 /**
Chris@5 43 * Constructs an AnnounceCommand object.
Chris@5 44 *
Chris@5 45 * @param string $text
Chris@5 46 * The text to be announced.
Chris@5 47 * @param string|null $priority
Chris@5 48 * (optional) The priority that will be used for the announcement. Defaults
Chris@5 49 * to NULL which will not set a 'priority' in the response sent to the
Chris@5 50 * client and therefore the JavaScript Drupal.announce() default of 'polite'
Chris@5 51 * will be used for the message.
Chris@5 52 */
Chris@5 53 public function __construct($text, $priority = NULL) {
Chris@5 54 $this->text = $text;
Chris@5 55 $this->priority = $priority;
Chris@5 56 }
Chris@5 57
Chris@5 58 /**
Chris@5 59 * {@inheritdoc}
Chris@5 60 */
Chris@5 61 public function render() {
Chris@5 62 $render = [
Chris@5 63 'command' => 'announce',
Chris@5 64 'text' => $this->text,
Chris@5 65 ];
Chris@5 66 if ($this->priority !== NULL) {
Chris@5 67 $render['priority'] = $this->priority;
Chris@5 68 }
Chris@5 69 return $render;
Chris@5 70 }
Chris@5 71
Chris@5 72 /**
Chris@5 73 * {@inheritdoc}
Chris@5 74 */
Chris@5 75 public function getAttachedAssets() {
Chris@5 76 $assets = new AttachedAssets();
Chris@5 77 $assets->setLibraries(['core/drupal.announce']);
Chris@5 78 return $assets;
Chris@5 79 }
Chris@5 80
Chris@5 81 }