You are here

public function TaskActionsForm::save in General Data Protection Regulation 8.2

Same name and namespace in other branches
  1. 8 modules/gdpr_tasks/src/Form/TaskActionsForm.php \Drupal\gdpr_tasks\Form\TaskActionsForm::save()
  2. 3.0.x modules/gdpr_tasks/src/Form/TaskActionsForm.php \Drupal\gdpr_tasks\Form\TaskActionsForm::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

modules/gdpr_tasks/src/Form/TaskActionsForm.php, line 175

Class

TaskActionsForm
Form controller for Task edit forms.

Namespace

Drupal\gdpr_tasks\Form

Code

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

  /* @var $entity \Drupal\gdpr_tasks\Entity\TaskInterface */
  $entity = $this->entity;
  if ($entity
    ->bundle() === 'gdpr_remove') {
    $errors = $this
      ->doRemoval($form_state);

    // Removals may have generated errors.
    // If this happens, combine the error messages and display them.
    if (count($errors) > 0) {
      $should_save = FALSE;
      array_map(function ($error) {
        $this
          ->messenger()
          ->addError($error);
      }, $errors);
      $form_state
        ->setRebuild();
    }
    else {
      $should_save = TRUE;
      $status = 'closed';
    }
  }
  else {

    // Queue task for completion.
    $this->queue
      ->createQueue();
    $this->queue
      ->createItem($entity
      ->id());
    $should_save = TRUE;
    $status = 'processed';
  }
  if ($should_save) {
    $entity->status = $status;
    $entity
      ->setProcessedById($this
      ->currentUser()
      ->id());
    $this
      ->messenger()
      ->addStatus('Task has been processed.');
    parent::save($form, $form_state);
  }
}