You are here

public function RevisionableContentEntityForm::save in Entity API 8

Same name and namespace in other branches
  1. 8.0 src/Form/RevisionableContentEntityForm.php \Drupal\entity\Form\RevisionableContentEntityForm::save()

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/RevisionableContentEntityForm.php, line 124

Class

RevisionableContentEntityForm
Extends the base entity form with revision support in the UI.

Namespace

Drupal\entity\Form

Code

public function save(array $form, FormStateInterface $form_state) {

  // Save as a new revision if requested to do so.
  if (!$form_state
    ->isValueEmpty('revision')) {
    $this->entity
      ->setNewRevision();
  }
  $insert = $this->entity
    ->isNew();
  $this->entity
    ->save();
  $context = [
    '@type' => $this->entity
      ->bundle(),
    '%info' => $this->entity
      ->label(),
  ];
  $logger = $this
    ->logger('content');
  $bundle_entity = $this
    ->getBundleEntity();
  $t_args = [
    '@type' => $bundle_entity ? $bundle_entity
      ->label() : 'None',
    '%info' => $this->entity
      ->label(),
  ];
  if ($insert) {
    $logger
      ->notice('@type: added %info.', $context);
    drupal_set_message($this
      ->t('@type %info has been created.', $t_args));
  }
  else {
    $logger
      ->notice('@type: updated %info.', $context);
    drupal_set_message($this
      ->t('@type %info has been updated.', $t_args));
  }
  if ($this->entity
    ->id()) {
    $form_state
      ->setValue('id', $this->entity
      ->id());
    $form_state
      ->set('id', $this->entity
      ->id());
    if ($this->entity
      ->getEntityType()
      ->hasLinkTemplate('collection')) {
      $form_state
        ->setRedirectUrl($this->entity
        ->toUrl('collection'));
    }
    else {
      $form_state
        ->setRedirectUrl($this->entity
        ->toUrl('canonical'));
    }
  }
  else {

    // In the unlikely case something went wrong on save, the entity will be
    // rebuilt and entity form redisplayed.
    drupal_set_message($this
      ->t('The entity could not be saved.'), 'error');
    $form_state
      ->setRebuild();
  }
}