You are here

public function WorkflowTypeForm::save in Workflow 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/WorkflowTypeForm.php, line 216

Class

WorkflowTypeForm
Provides the base form for workflow add and edit forms.

Namespace

Drupal\workflow\Form

Code

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

  /** @var \Drupal\workflow\Entity\Workflow $entity */
  $entity = $this->entity;

  // Prevent leading and trailing spaces.
  $entity
    ->set('label', trim($entity
    ->label()));
  $entity
    ->set('options', [
    'name_as_title' => $form_state
      ->getValue('name_as_title'),
    'fieldset' => $form_state
      ->getValue('fieldset'),
    'options' => $form_state
      ->getValue('options'),
    'schedule_enable' => $form_state
      ->getValue('schedule_enable'),
    'schedule_timezone' => $form_state
      ->getValue('schedule_timezone'),
    'always_update_entity' => $form_state
      ->getValue('always_update_entity'),
    'comment_log_node' => $form_state
      ->getValue('comment_log_node'),
    'watchdog_log' => $form_state
      ->getValue('watchdog_log'),
  ]);
  $status = parent::save($form, $form_state);
  $action = $status == SAVED_UPDATED ? 'updated' : 'added';

  // Tell the user we've updated the data.
  $args = [
    '%label' => $entity
      ->label(),
    '%action' => $action,
    'link' => $entity
      ->toLink($this
      ->t('Edit'))
      ->toString(),
  ];
  $this
    ->messenger()
    ->addStatus($this
    ->t('Workflow %label has been %action. Please maintain the permissions,
       states and transitions.', $args));
  $this
    ->logger('workflow')
    ->notice('Workflow %label has been %action.', $args);
  if ($status == SAVED_NEW) {
    $form_state
      ->setRedirect('entity.workflow_type.edit_form', [
      'workflow_type' => $entity
        ->id(),
    ]);
  }
}