Skip to content

Instantly share code, notes, and snippets.

@signalpoint
Last active February 5, 2025 19:23
Show Gist options
  • Select an option

  • Save signalpoint/40a3add1ccc385c558606353ebdcde00 to your computer and use it in GitHub Desktop.

Select an option

Save signalpoint/40a3add1ccc385c558606353ebdcde00 to your computer and use it in GitHub Desktop.
Drupal 8 Set Message Example
<?php
// The drupal_set_message() function is being deprecated!
// @see https://api.drupal.org/api/drupal/core%21includes%21bootstrap.inc/function/drupal_set_message/8.5.x
// > Deprecated in Drupal 8.5.0 and will be removed before Drupal 9.0.0.
// > Use \Drupal\Core\Messenger\MessengerInterface::addMessage() instead.
// In some custom code.
\Drupal::messenger()->addMessage('Say something else');
// When trying to print out a simple var.
\Drupal::messenger()->addMessage(print_r($stuff, TRUE));
// In a Drupal 8 Form's submitForm() handler:
$this->messenger()->addMessage($this->t('Hello world.'));
@frazras
Copy link
Copy Markdown

frazras commented Mar 18, 2019

Here are all the set message alternative for message types.

<?php
// Add message (defaults to "status" message type).
$this->messenger()->addMessage('Hello world');

// Add specific type of message.
$this->messenger()->addMessage('Hello world', 'custom');
$this->messenger()->addError('Hello world');
$this->messenger()->addStatus('Hello world');
$this->messenger()->addWarning('Hello world');
?>

@heitoralthmann
Copy link
Copy Markdown

Here's an example using dependency injection: https://www.drupal.org/node/2774931

@danrod96-new
Copy link
Copy Markdown

Thanks for sharing this (a bit late though)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment