MessageCommand.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. <?php
  2. namespace Drupal\Core\Ajax;
  3. use Drupal\Core\Asset\AttachedAssets;
  4. /**
  5. * AJAX command for a JavaScript Drupal.message() call.
  6. *
  7. * Developers should be extra careful if this command and
  8. * \Drupal\Core\Ajax\AnnounceCommand are included in the same response. Unless
  9. * the `announce` option is set to an empty string (''), this command will
  10. * result in the message being announced to screen readers. When combined with
  11. * AnnounceCommand, this may result in unexpected behavior. Manual testing with
  12. * a screen reader is strongly recommended.
  13. *
  14. * Here are examples of how to suppress announcements:
  15. * @code
  16. * $command = new MessageCommand("I won't be announced", NULL, [
  17. * 'announce' => '',
  18. * ]);
  19. * @endcode
  20. *
  21. * @see \Drupal\Core\Ajax\AnnounceCommand
  22. *
  23. * @ingroup ajax
  24. */
  25. class MessageCommand implements CommandInterface, CommandWithAttachedAssetsInterface {
  26. /**
  27. * The message text.
  28. *
  29. * @var string
  30. */
  31. protected $message;
  32. /**
  33. * Whether to clear previous messages.
  34. *
  35. * @var bool
  36. */
  37. protected $clearPrevious;
  38. /**
  39. * The query selector for the element the message will appear in.
  40. *
  41. * @var string
  42. */
  43. protected $wrapperQuerySelector;
  44. /**
  45. * The options passed to Drupal.message().add().
  46. *
  47. * @var array
  48. */
  49. protected $options;
  50. /**
  51. * Constructs a MessageCommand object.
  52. *
  53. * @param string $message
  54. * The text of the message.
  55. * @param string|null $wrapper_query_selector
  56. * The query selector of the element to display messages in when they
  57. * should be displayed somewhere other than the default.
  58. * @see Drupal.Message.defaultWrapper()
  59. * @param array $options
  60. * The options passed to Drupal.message().add().
  61. * @param bool $clear_previous
  62. * If TRUE, previous messages will be cleared first.
  63. */
  64. public function __construct($message, $wrapper_query_selector = NULL, array $options = [], $clear_previous = TRUE) {
  65. $this->message = $message;
  66. $this->wrapperQuerySelector = $wrapper_query_selector;
  67. $this->options = $options;
  68. $this->clearPrevious = $clear_previous;
  69. }
  70. /**
  71. * {@inheritdoc}
  72. */
  73. public function render() {
  74. return [
  75. 'command' => 'message',
  76. 'message' => $this->message,
  77. 'messageWrapperQuerySelector' => $this->wrapperQuerySelector,
  78. 'messageOptions' => $this->options,
  79. 'clearPrevious' => $this->clearPrevious,
  80. ];
  81. }
  82. /**
  83. * {@inheritdoc}
  84. */
  85. public function getAttachedAssets() {
  86. $assets = new AttachedAssets();
  87. $assets->setLibraries(['core/drupal.message']);
  88. return $assets;
  89. }
  90. }