You are here

public function WebformScheduledTaskForm::save in Webform Scheduled Tasks 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/WebformScheduledTaskForm.php, line 178

Class

WebformScheduledTaskForm
The scheduled task form.

Namespace

Drupal\webform_scheduled_tasks\Form

Code

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

  /** @var \Drupal\webform_scheduled_tasks\Entity\WebformScheduledTaskInterface $schedule */
  $schedule = $this->entity;
  if (!$schedule
    ->isNew()) {
    $this
      ->submitPluginForm($form['task_settings'], $schedule
      ->getTaskPlugin(), $form, $form_state);
    $this
      ->submitPluginForm($form['result_set_settings'], $schedule
      ->getResultSetPlugin(), $form, $form_state);
  }

  // The next time the task runs isn't a property on the config entity, so it
  // must be set manually.
  if ($next_run = $form_state
    ->getValue('next_run')) {
    $schedule
      ->setNextTaskRunDate($next_run
      ->getTimestamp());
  }

  // On the first save, when the type plugins are set, redirect to the edit
  // form to complete adding the settings for those plugin types.
  $redirect_collection = $this->entity
    ->isNew() ? 'edit-form' : 'collection';
  parent::save($form, $form_state);
  $this
    ->messenger()
    ->addStatus($this
    ->t('The scheduled task was saved successfully.'));
  $form_state
    ->setRedirect($this->entity
    ->toUrl($redirect_collection)
    ->getRouteName(), $this->entity
    ->toUrl($redirect_collection)
    ->getRouteParameters());
}