ICT:D Removing Node Links from Status Messages
Drupal 11.3 Technical Note: Removing Node Links from Status Messages
Overview
In the Heritage Project, historians work primarily within Views. When a node is saved, Drupal 11 default behavior displays a status message: "Node [Title] has been updated," where [Title] is a link to the node page. To prevent users from navigating away from the View, this link is replaced with plain text.
Technical Solution
We use a custom module to intercept the Messenger service. We have added a "Role Check" so that Administrators still get the link for utility, while other roles (Historians) see only text.
Step 1: Automatic Module Generation
Using Drush 13 (standard for Drupal 11.3), generate the module scaffold.
drush generate module
# Name: Heritage Tweaks
# Machine name: heritage_tweaks
# Package: Heritage
# Create .module file: Yes
Step 2: Implementation (heritage_tweaks.module)
The logic uses a form alter to append a custom submit handler. This code replaces the linked message with a plain-text version for non-admins.
<?php
/**
* @file
* Primary module hooks for Heritage Tweaks module.
*/
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Messenger\MessengerInterface;
/**
* Implements hook_form_BASE_FORM_ID_alter() for node_form.
* This targets all node edit/create forms.
*/
function heritage_tweaks_form_node_form_alter(&$form, FormStateInterface $form_state, $form_id) {
// Append our custom cleaner to the end of the submit array.
$form['actions']['submit']['#submit'][] = 'heritage_tweaks_clean_save_message';
}
/**
* Clears the default linked save message and replaces it with plain text.
*/
function heritage_tweaks_clean_save_message($form, FormStateInterface $form_state) {
// 1. Check if the current user is an Administrator.
$current_user = \Drupal::currentUser();
if ($current_user->hasPermission('administer nodes')) {
// If they are an admin, stop here and keep the default linked message.
return;
}
$messenger = \Drupal::messenger();
$node = $form_state->getFormObject()->getEntity();
// 2. Wipe the standard status messages (which contains the link).
$messenger->deleteByType(MessengerInterface::TYPE_STATUS);
// 3. Re-add the status as plain text using the node's label.
// Using %title ensures it is rendered as text, not a link.
$messenger->addStatus(t('@type %title has been updated.', [
'@type' => $node->type->entity->label(),
'%title' => $node->label(),
]));
}
Step 3: Activation
Enable the module and clear the cache.
drush en heritage_tweaks -y
drush cr
Why this is Drupal 11.3 Compatible
- Service-Oriented: It uses `\Drupal::messenger()` and `\Drupal::currentUser()` instead of deprecated procedural functions.
- Role-Aware: Uses `hasPermission()` which is the most stable way to differentiate users in Drupal 11.
- PSR-4 Compliant: By using Drush 13 to generate the files, the directory structure matches Drupal 11's autoloader requirements.