Drupal - How do I programmatically change a system message?
I was seeking a similar solution and found this great post describing using a hook_preprocess_HOOK(), in this case, hook_preprocess_status_messages() - https://stackoverflow.com/questions/38788080/remove-default-message-in-drupal-8
For my purpose to override a message, I use a simple strpos in the logic:
/**
* Implements hook_preprocess_HOOK().
*
* @param $variables
*/
function mymodule_preprocess_status_messages(&$variables) {
if(isset($variables['message_list']['status'])){
$status_messages = $variables['message_list']['status'];
foreach($status_messages as $delta => $message) {
if (strpos((string) $message, 'The message that I am expecting') !== FALSE) {
$variables['message_list']['status'][$delta] = "Some different message text";
}
}
}
}
System messages are stored in a Symfony Flashbag, so there is no need to hack session variables, which can change from version to version. In Drupal 8.5 a new Messenger service was introduced to encapsulate the symfony flash bag and you can delete messages with deleteAll():
\Drupal::messenger()->deleteAll();
I think the answer might be not to use hook_entity_insert(). By that time, it is too late: the content has already been created, so the system will create the message accordingly.
As the hook_entity_insert documentation says: hook_entity_insert responds to the creation of a new entity.
Maybe try another hook from the Entity API, one that does not cause the unwanted message to be generated.