Allow modules to alter messages during Node save

Created on 23 February 2020, almost 5 years ago
Updated 14 December 2023, about 1 year ago

Problem/Motivation

In many projects - specifically when entity forms are displayed on front end - default messages from Node module are not relevant to contributors and should not be displayed.

Currently there is no easy way to alter messages displayed after a Node entity was saved.

As a site admin, I want to be able to disable the default "overcomplicated" messages displayed after Node save (currently, something like Article "Title of the article" has been updated).

As a developer, I want to be able to do custom things during Node save - e.g. display custom messages.

Proposed resolution

We should :

- add an option to enable/disable message display on save. Since website can have many different node types, we should add this option on the NodeType entity.
- add the opportunity to developper to alter things during NodeForm::save();. Until Event triggers are implemented in Entity's operations/forms, I suggest to add a custom hook.

Remaining tasks

  • Create a patch
  • Add test for the new NodeType option
  • Add test fot the new hook in NodeSave

User interface changes

New option in NodeType admin form.

API changes

None.

Related Issues

Issues:

Forum post about these messages:

Feature request
Status

Active

Version

11.0 🔥

Component
Node system 

Last updated about 23 hours ago

No maintainer
Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • 🇩🇰Denmark ressa Copenhagen

    This is a great idea! It would be awesome to be able to alter the text at some point, and hope it's ok that I reopen the issue?

  • 🇩🇰Denmark ressa Copenhagen

    As a workaround until (or if) this lands, here's one method to tweak the message, from a MR for Simplenews:

    use Drupal\Core\Entity\EntityForm;
    
    /**
     * Implements hook_form_alter().
     *
     * Adds link to preview and send page above a newsletter.
     */
    function simplenews_form_alter(&$form, &$form_state, $form_id) {
      $forms = [
        'node_simplenews_issue_form',
        'node_simplenews_issue_edit_form',
      ];
      $admin_or_send_rights = (\Drupal::currentUser()->hasPermission('administer newsletters') || \Drupal::currentUser()->hasPermission('send newsletter'));
      if (in_array($form_id, $forms) && $admin_or_send_rights) {
        // Adding custom callback to customize status message.
        $form['actions']['submit']['#submit'][] = '_post_node_save_callback';
      }
    }
    
    /**
     * 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();
        $node_id = $node->id();
        \Drupal::messenger()->addStatus(t('Preview or send it via the <a href="/node/' . $node_id .'/simplenews">Newsletter</a> tab, administer under <a href="/admin/content/simplenews">Newsletter issues</a>.'));
      }
    }
    

    Based on the great blog post Customize/override the “node has been created/updated” status message on Drupal 9 by Barbara Bombachini .

Production build 0.71.5 2024