TransactionExecuteForm.php in Transaction 8
File
src/Form/TransactionExecuteForm.php
View source
<?php
namespace Drupal\transaction\Form;
use Drupal\Core\Entity\ContentEntityConfirmFormBase;
use Drupal\Core\Form\FormStateInterface;
class TransactionExecuteForm extends ContentEntityConfirmFormBase {
public function getQuestion() {
return $this
->t('Execute %label transaction?', [
'%label' => $this->entity
->label(),
]);
}
public function getDescription() {
$transaction = $this
->getEntity();
$transaction_type = $transaction
->get('type')->entity;
return $transaction_type
->getPlugin()
->getExecutionIndications($transaction) ?: parent::getDescription();
}
public function getCancelUrl() {
$entity = $this
->getEntity();
return $entity
->getTargetEntityId() ? $entity
->toUrl('collection', [
'target_entity' => $entity
->getTargetEntityId(),
]) : $entity
->toUrl();
}
public function submitForm(array &$form, FormStateInterface $form_state) {
parent::submitForm($form, $form_state);
$transaction = $this
->getEntity();
$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'));
}
public function getConfirmText() {
return $this
->t('Execute transaction');
}
}