You are here

public function DataPolicyAddForm::save in Data Policy 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/DataPolicyAddForm.php, line 123

Class

DataPolicyAddForm
Class DataPolicyAddForm.

Namespace

Drupal\data_policy\Form

Code

public function save(array $form, FormStateInterface $form_state) {
  if ($this->routeName === 'entity.data_policy.revision_add') {
    $this->entity
      ->setNewRevision(TRUE);
    $active_revision = !empty($form_state
      ->getValue('active_revision'));
    if (!$active_revision) {
      $this->entity
        ->isDefaultRevision(FALSE);
    }
    $this->entity
      ->set('name', $form_state
      ->getValue('name'));
    $this->entity
      ->set('field_description', $form_state
      ->getValue('field_description'));
    $this->entity
      ->set('revision_log_message', $form_state
      ->getValue('revision_log_message'));
    $this->entity
      ->setRevisionCreationTime($this->time
      ->getRequestTime());
    $this->entity
      ->setRevisionUserId($this
      ->currentUser()
      ->id());
    $this->entity
      ->save();
    $this
      ->messenger()
      ->addStatus($this
      ->t('Created new revision.'));
    $form_state
      ->setRedirect('entity.data_policy.version_history', [
      'entity_id' => $this->entityId,
    ]);
  }
  else {
    return parent::save($form, $form_state);
  }
}