NodeForm::save

public NodeForm::save(array $form, FormStateInterface $form_state)

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

core/modules/node/src/NodeForm.php, line 353

Class

NodeForm
Form handler for the node edit forms.

Namespace

Drupal\node

Code

public function save(array $form, FormStateInterface $form_state) {
  $node = $this->entity;
  $insert = $node->isNew();
  $node->save();
  $node_link = $node->link($this->t('View'));
  $context = array('@type' => $node->getType(), '%title' => $node->label(), 'link' => $node_link);
  $t_args = array('@type' => node_get_type_label($node), '%title' => $node->link($node->label()));

  if ($insert) {
    $this->logger('content')->notice('@type: added %title.', $context);
    drupal_set_message(t('@type %title has been created.', $t_args));
  }
  else {
    $this->logger('content')->notice('@type: updated %title.', $context);
    drupal_set_message(t('@type %title has been updated.', $t_args));
  }

  if ($node->id()) {
    $form_state->setValue('nid', $node->id());
    $form_state->set('nid', $node->id());
    if ($node->access('view')) {
      $form_state->setRedirect(
      'entity.node.canonical', 
      array('node' => $node->id())
      );
    }
    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());
  }
  else {
    // In the unlikely case something went wrong on save, the node will be
    // rebuilt and node form redisplayed the same way as in preview.
    drupal_set_message(t('The post could not be saved.'), 'error');
    $form_state->setRebuild();
  }
}
doc_Drupal
2016-10-29 09:30:46
Comments
Leave a Comment

Please login to continue.