Create a trait, or add methods to EntityForm, for logging and messages for entity add/update

Created on 21 December 2017, over 6 years ago
Updated 30 January 2023, over 1 year ago

Problem/Motivation

On #2923558: Confirm message should include a link to the created/edited topic β†’ , we found ourselves repeatedly writing fairly generic code in entity form save() methods to do a drupal_set_message() [which in 8.5.x should use the Messenger service of course) and Logger::notice().

It seems like it would be good if there was a trait, so that these messages could be standardized.

As an example of what we already have in Core, there is EntityDeleteFormTrait::getDeletionMessage(), which calculates the message:

protected function getDeletionMessage() {
  $entity = $this->getEntity();
  return $this->t('The @entity-type %label has been deleted.', [
    '@entity-type' => $entity->getEntityType()->getLowercaseLabel(),
    '%label' => $entity->label(),
  ]);
}

For add and update messages, it is desirable that, if possible, the entity label be a link to the entity. On the above issue, we did that by doing this in the save() method:

    $link = $this->entity->toLink(NULL, 'canonical')->toString();
    $ops_link = $this->entity->toLink($this->t('View'), 'canonical')->toString();
    $args = ['@link' => $link];
    if ($status == SAVED_UPDATED) {
      $message = $this->t('The help topic @link has been updated.', $args);
    }
    else {
      $message = $this->t('The help topic @link has been added.', $args);
    }

    drupal_set_message($message);
    $this->getLogger('config_help')->notice($message, $args + [
      'link' => $ops_link,
    ]);

Proposed resolution

a) Figure out what methods would be useful on the trait, and make a trait.
b) Update entities to use the trait instead of devising their own messages.

Remaining tasks

Make a patch.

User interface changes

More unified messages on entity add/update operations.

API changes

New trait that entities can use.

Data model changes

None.

✨ Feature request
Status

Needs work

Version

10.1 ✨

Component
EntityΒ  β†’

Last updated 28 minutes ago

Created by

πŸ‡ΊπŸ‡ΈUnited States jhodgdon Spokane, WA, USA

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.

  • The Needs Review Queue Bot β†’ tested this issue. It either no longer applies to Drupal core, or fails the Drupal core commit checks. Therefore, this issue status is now "Needs work".

    Apart from a re-roll or rebase, this issue may need more work to address feedback in the issue or MR comments. To progress an issue, incorporate this feedback as part of the process of updating the issue. This helps other contributors to know what is outstanding.

    Consult the Drupal Contributor Guide β†’ to find step-by-step guides for working with issues.

Production build 0.69.0 2024