How to replace the deprecated drupal_set_message() function!

Submitted by admin-matapedia on

Since Drupal 9, the Drupal deprecated function drupal_set_message() is removed !

In replacement, you can use the Core Messenger service. You can call it with a static access or an Injection dependency.

// Static access
\Drupal::messenger()->addMessage('A message to display!');

// Injection dependency
 $this->messenger()->addMessage('A message to display!');

You can find a dependency injection example on Drupal website : https://www.drupal.org/node/2774931

Enjoy!

Tags
Categories