You are here

public function TransactionExecuteForm::submitForm in Transaction 8

This is the default entity object builder function. It is called before any other submit handler to build the new entity object to be used by the following submit handlers. At this point of the form workflow the entity is validated and the form state can be updated, this way the subsequently invoked handlers can retrieve a regular entity object to act on. Generally this method should not be overridden unless the entity requires the same preparation for two actions, see \Drupal\comment\CommentForm for an example with the save and preview actions.

Parameters

array $form: An associative array containing the structure of the form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

Overrides ContentEntityForm::submitForm

File

src/Form/TransactionExecuteForm.php, line 46

Class

TransactionExecuteForm
Provides a form for transaction execution.

Namespace

Drupal\transaction\Form

Code

public function submitForm(array &$form, FormStateInterface $form_state) {
  parent::submitForm($form, $form_state);

  /** @var \Drupal\transaction\TransactionInterface $transaction */
  $transaction = $this
    ->getEntity();

  // ContentEntityForm::buildEntity sets the entity validation required, but
  // ContentEntityConfirmFormBase overrides the validateForm method, so the
  // entity finally remains in a state that throws an "Entity validation was
  // skipped" exception when trying to save it. Unsetting the validation
  // required here as not needed.
  $transaction
    ->setValidationRequired(FALSE);
  if ($transaction
    ->execute()) {
    $this
      ->messenger()
      ->addStatus($this
      ->t('Transaction @label executed successfully.', [
      '@label' => $transaction
        ->label(),
    ]));
  }
  else {
    $this
      ->messenger()
      ->addError($transaction
      ->getResultMessage() ?: $this
      ->t('There was an error executing @label transaction.', [
      '@label' => $transaction
        ->label(),
    ]));
  }
  $form_state
    ->setRedirectUrl($transaction
    ->toUrl('collection'));
}