public function MessageForm::save in Message UI 8
Form submission handler for the 'save' action.
Normally this method should be overridden to provide specific messages to the user and redirect the form after the entity has been saved.
Parameters
array $form: An associative array containing the structure of the form.
\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.
Return value
int Either SAVED_NEW or SAVED_UPDATED, depending on the operation performed.
Overrides EntityForm::save
File
- src/
Form/ MessageForm.php, line 249
Class
- MessageForm
- Form controller for the message_ui entity edit forms.
Namespace
Drupal\message_ui\FormCode
public function save(array $form, FormStateInterface $form_state) {
/* @var $message Message */
$message = $this->entity;
$insert = $message
->isNew();
$message
->save();
// Set up message link and status message contexts.
$message_link = $message
->toLink($this
->t('View'))
->toString();
$context = [
'@type' => $message
->getTemplate()
->id(),
'%title' => 'Message:' . $message
->id(),
'link' => $message_link,
];
$t_args = [
'@type' => $message
->getEntityType()
->getLabel(),
'%title' => 'Message:' . $message
->id(),
];
// Display newly created or updated message depending on if new entity.
if ($insert) {
$this
->logger('content')
->notice('@type: added %title.', $context);
$this
->messenger()
->addMessage(t('@type %title has been created.', $t_args));
}
else {
$this
->logger('content')
->notice('@type: updated %title.', $context);
$this
->messenger()
->addMessage(t('@type %title has been updated.', $t_args));
}
// Redirect to message view display if user has access.
if ($message
->id()) {
$form_state
->setValue('mid', $message
->id());
$form_state
->set('mid', $message
->id());
if ($message
->access('view')) {
$form_state
->setRedirect('entity.message.canonical', [
'message' => $message
->id(),
]);
}
else {
$form_state
->setRedirect('<front>');
}
// @todo : for node they clear temp store here, but perhaps unused with
// message.
}
else {
// In the unlikely case something went wrong on save, the message will be
// rebuilt and message form redisplayed.
$this
->messenger()
->addMessage(t('The message could not be saved.'), 'error');
$form_state
->setRebuild();
}
}