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.