class MessageCommand implements CommandInterface, CommandWithAttachedAssetsInterface (View source)

AJAX command for a JavaScript Drupal.message() call.

Developers should be extra careful if this command and \Drupal\Core\Ajax\AnnounceCommand are included in the same response. Unless the announce option is set to an empty string (''), this command will result in the message being announced to screen readers. When combined with AnnounceCommand, this may result in unexpected behavior. Manual testing with a screen reader is strongly recommended.

Here are examples of how to suppress announcements:

Properties

protected string $message

The message text.

protected bool $clearPrevious

Whether to clear previous messages.

protected string $wrapperQuerySelector

The query selector for the element the message will appear in.

protected array $options

The options passed to Drupal.message().add().

Methods

__construct(string $message, string|null $wrapper_query_selector = NULL, array $options = [], bool $clear_previous = TRUE)

Constructs a MessageCommand object.

render()

Return an array to be run through json_encode and sent to the client.

getAttachedAssets()

Gets the attached assets.

Details

__construct(string $message, string|null $wrapper_query_selector = NULL, array $options = [], bool $clear_previous = TRUE)

Constructs a MessageCommand object.

Parameters

string $message

The text of the message.

string|null $wrapper_query_selector

The query selector of the element to display messages in when they should be displayed somewhere other than the default. Drupal.Message.defaultWrapper()

array $options

The options passed to Drupal.message().add().

bool $clear_previous

If TRUE, previous messages will be cleared first.

render()

Return an array to be run through json_encode and sent to the client.

AttachedAssets|null getAttachedAssets()

Gets the attached assets.

Return Value

AttachedAssets|null

The attached assets for this command.