Customize/override the “node has been created/updated” status message on Drupal 9

Barbara Bombachini
Code Snippets
Published in
1 min readAug 15, 2022

The last few years I have been working with Drupal so I’ve been using a lot more the provided tools for support than medium, but as I hope to save some of the snippets for myself anyway (that’s the idea of this blog really), here’s a comeback!

This one I want to share is really simple but it took me some time to find the right moment to hook on the process. At first I thought that a `hook_entity_update` would do it, but the status message is set right after it.

I’ve also tried hook_preprocess_status_message with no luck.

Here’s how I got it working:

  1. In a hook form alter, add a custom submit callback.
/**
* Implements hook_form_alter().
*/
function yourmodule_form_alter(&$form, &$form_state, $form_id) {
$forms = [
'node_node_1_edit_form',
'node_node_2_edit_form',
'node_node_3_edit_form',
];
if (in_array($form_id, $forms)) {
// Adding custom callback to customize status message.
$form['actions']['submit']['#submit'][] = '_post_node_save_callback';
}}

2. In your submit callback, delete the setup messages by type and yours.

use Drupal\Core\Entity\EntityForm;/**
* Custom callback to customize status message.
*/
function _post_node_save_callback($form, &$form_state) {
$form_object = $form_state->getFormObject();
if ($form_object instanceof EntityForm) {
$node = $form_object->getEntity();
$args = ['%title' => $node->toLink()->toString()];
\Drupal::messenger()->deleteByType('status');
\Drupal::messenger()->addStatus(t('%title has been updated.', $args));

}
}

In my case, the node title had the same name as the node type, so I just need to remove the @type from the string.

--

--