Allow modules to alter messages during Node save

Created on 23 February 2020, about 5 years ago
Updated 14 December 2023, over 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 19 minutes 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 .

  • Status changed to Closed: won't fix 12 days ago
  • 🇦🇺Australia acbramley

    All core entities have something like this in their entity form's save method. Modules can override an entity types form class and subclass it to override the save method and add their own messages. I don't think customising messages is something that could/would ever be able to be supported in core.

    For simple string changes, you can also use the locale_custom_strings_en setting, e.g you could map:
    '@type %title has been updated.' => '@type %title has been updated, cool!

Production build 0.71.5 2024