Question
How to show messages to user?
Solution
Drupal 8 Messenger Service used to show messages to the user. There are four different types of message available, each of which is printed with other messages of the same type and on a different coloured background.
// Green status message.
\Drupal::messenger()->addStatus('show a status to the user.');
// Yellow warning message.
\Drupal::messenger()->addWarning('A warning about something!');
// Red error message.
\Drupal::messenger()->addError('An error happened!');
or you can addMessage() method
// show message: TYPE_MESSAGE is ('status', 'warning' or 'error') by default is "status"
\Drupal::messenger()->addMessage('Message printed to a user.', TYPE_MESSAGE);