ICT:D Removing Node Links from Status Messages: Difference between revisions
Created page with "= 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 se..." |
No edit summary |
||
| Line 80: | Line 80: | ||
* '''PSR-4 Compliant:''' By using Drush 13 to generate the files, the directory structure matches Drupal 11's autoloader requirements. | * '''PSR-4 Compliant:''' By using Drush 13 to generate the files, the directory structure matches Drupal 11's autoloader requirements. | ||
== Configuration & Deployment == | |||
Because this fix is implemented via a '''custom module''', it behaves differently than configuration changes (like Views or Content Types). | |||
=== 1. Version Control === | |||
Add your new module to your Git repository so it can be deployed to the heritage project server. | |||
<source lang="bash"> | |||
git add web/modules/custom/heritage_tweaks | |||
git commit -m "ICT: Fix node link in status messages for historians" | |||
</source> | |||
=== 2. Deployment Command === | |||
When deploying to the server, ensure you run the installation command to activate the module on the live site: | |||
<source lang="bash"> | |||
drush en heritage_tweaks -y | |||
drush cr | |||
</source> | |||
=== 3. Configuration Management === | |||
If you eventually add settings to this module (like a configuration form to change the message text), remember to export them: | |||
<source lang="bash"> | |||
drush cex -y | |||
</source> | |||
[[Category:Drupal 11 Deployment]] | |||
[[Category:Drupal 11]] [[Category:Heritage Project Documentation]] [[Category:ICT:pages]] | [[Category:Drupal 11]] [[Category:Heritage Project Documentation]] [[Category:ICT:pages]] | ||
Latest revision as of 15:17, 22 March 2026
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.
Configuration & Deployment
Because this fix is implemented via a custom module, it behaves differently than configuration changes (like Views or Content Types).
1. Version Control
Add your new module to your Git repository so it can be deployed to the heritage project server.
git add web/modules/custom/heritage_tweaks
git commit -m "ICT: Fix node link in status messages for historians"
2. Deployment Command
When deploying to the server, ensure you run the installation command to activate the module on the live site:
drush en heritage_tweaks -y
drush cr
3. Configuration Management
If you eventually add settings to this module (like a configuration form to change the message text), remember to export them:
drush cex -y