function NodeForm::save
Overrides EntityForm::save
File
-
core/
modules/ node/ src/ Form/ NodeForm.php, line 280
Class
- NodeForm
- Form handler for the node edit forms.
Namespace
Drupal\node\FormCode
public function save(array $form, FormStateInterface $form_state) {
$node = $this->entity;
$insert = $node->isNew();
try {
$node->save();
$node_link = $node->toLink($this->t('View'))
->toString();
$context = [
'@type' => $node->getType(),
'%title' => $node->label(),
'link' => $node_link,
];
$t_args = [
'@type' => $node->getBundleEntity()
->label(),
'%title' => $node->access('view') ? $node->toLink()
->toString() : $node->label(),
];
if ($insert) {
$this->logger('content')
->info('@type: added %title.', $context);
$this->messenger()
->addStatus($this->t('@type %title has been created.', $t_args));
}
else {
$this->logger('content')
->info('@type: updated %title.', $context);
$this->messenger()
->addStatus($this->t('@type %title has been updated.', $t_args));
}
$form_state->setValue('nid', $node->id());
$form_state->set('nid', $node->id());
if ($node->access('view')) {
$form_state->setRedirect('entity.node.canonical', [
'node' => $node->id(),
], [
'language' => $node->language(),
]);
}
else {
$form_state->setRedirect('<front>');
}
// Remove the preview entry from the temp store, if any.
$store = $this->tempStoreFactory
->get('node_preview');
$store->delete($node->uuid());
} catch (\Exception $e) {
// In the unlikely case something went wrong on save, the node will be
// rebuilt and node form redisplayed.
$this->messenger()
->addError($this->t('The content could not be saved. Contact the site administrator if the problem persists.'));
// It's likely that this exception is an EntityStorageException in which
// case we won't have the actual backtrace available. Attempt to get the
// previous exception if available to include the backtrace.
$e = $e->getPrevious() ?: $e;
\Drupal::logger('node')->error('%type saving node form: @message in %function (line %line of %file) @backtrace_string.', Error::decodeException($e));
$form_state->setRebuild();
}
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.