annotate core/lib/Drupal/Core/Ajax/AnnounceCommand.php @ 19:fa3358dc1485 tip

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