You are here

public function WorkspaceForm::save in Workspace 8.2

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/WorkspaceForm.php, line 126

Class

WorkspaceForm
Form controller for the workspace edit forms.

Namespace

Drupal\workspace\Form

Code

public function save(array $form, FormStateInterface $form_state) {
  $workspace = $this->entity;
  $workspace
    ->setNewRevision(TRUE);
  $status = $workspace
    ->save();
  $info = [
    '%info' => $workspace
      ->label(),
  ];
  $context = [
    '@type' => $workspace
      ->bundle(),
    '%info' => $workspace
      ->label(),
  ];
  $logger = $this
    ->logger('workspace');
  if ($status == SAVED_UPDATED) {
    $logger
      ->notice('@type: updated %info.', $context);
    $this->messenger
      ->addMessage($this
      ->t('Workspace %info has been updated.', $info));
  }
  else {
    $logger
      ->notice('@type: added %info.', $context);
    $this->messenger
      ->addMessage($this
      ->t('Workspace %info has been created.', $info));
  }
  if ($workspace
    ->id()) {
    $form_state
      ->setValue('id', $workspace
      ->id());
    $form_state
      ->set('id', $workspace
      ->id());
    $collection_url = $workspace
      ->toUrl('collection');
    $redirect = $collection_url
      ->access() ? $collection_url : Url::fromRoute('<front>');
    $form_state
      ->setRedirectUrl($redirect);
  }
  else {
    $this->messenger
      ->addError($this
      ->t('The workspace could not be saved.'));
    $form_state
      ->setRebuild();
  }
}