You are here

public function TransactionForm::save in Transaction 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/TransactionForm.php, line 82

Class

TransactionForm
Form controller for the transaction entity.

Namespace

Drupal\transaction\Form

Code

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

  /** @var \Drupal\transaction\TransactionInterface $transaction */
  $transaction = $this->entity;

  // Execute if the user indicated to do so.
  if ($transaction
    ->getType()
    ->getOption('execution') == TransactionTypeInterface::EXECUTION_ASK && $form_state
    ->getValue('execute', FALSE) && $transaction
    ->isPending()) {
    $executed = $this->entity
      ->execute(FALSE);
  }

  // Save the transaction.
  $saved = parent::save($form, $form_state);
  $msg_args = [
    '@type' => $transaction
      ->getType()
      ->label(),
    '%description' => $transaction
      ->label(),
  ];
  $this
    ->messenger()
    ->addStatus($saved == SAVED_NEW ? $this
    ->t('New transaction of type @type has been created.', $msg_args) : $this
    ->t('Transaction %description updated.', $msg_args));

  // Executed transaction post save actions.
  if (isset($executed)) {

    // Execution result message.
    if ($result_code = $transaction
      ->getResultCode()) {
      $this
        ->messenger()
        ->addMessage($transaction
        ->getResultMessage(), $result_code > 0 ? MessengerInterface::TYPE_STATUS : MessengerInterface::TYPE_ERROR);
    }
  }
  $form_state
    ->setRedirectUrl($this->entity
    ->toUrl('collection'));
  return $saved;
}