TransactionForm.php in Transaction 8
File
src/Form/TransactionForm.php
View source
<?php
namespace Drupal\transaction\Form;
use Drupal\Core\Entity\ContentEntityForm;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Messenger\MessengerInterface;
use Drupal\transaction\TransactionTypeInterface;
use Drupal\Core\Entity\ContentEntityInterface;
class TransactionForm extends ContentEntityForm {
public function buildForm(array $form, FormStateInterface $form_state, ContentEntityInterface $target_entity = NULL) {
$transaction = $this->entity;
if ($target_entity) {
$transaction
->setTargetEntity($target_entity);
}
$form = parent::buildForm($form, $form_state);
$form['advanced'] = [
'#type' => 'vertical_tabs',
'#weight' => 99,
];
$form['transaction_authoring'] = [
'#type' => 'details',
'#title' => $this
->t('Transaction authoring'),
'#open' => TRUE,
'#group' => 'advanced',
];
$form['uid']['#group'] = 'transaction_authoring';
$form['created']['#group'] = 'transaction_authoring';
if ($transaction
->getType()
->getOption('execution') == TransactionTypeInterface::EXECUTION_ASK && $transaction
->isPending()) {
$form['execute'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Execute'),
'#weight' => 99,
];
}
return $form;
}
protected function prepareEntity() {
$transaction = $this->entity;
if (!$transaction
->getTargetEntityId()) {
$route_options = $this
->getRouteMatch()
->getRouteObject()
->getOptions();
$target_entity_type_id = isset($route_options['_transaction_target_entity_type_id']) ? $route_options['_transaction_target_entity_type_id'] : $transaction
->getType()
->getTargetEntityTypeId();
if ($target_entity = $this
->getRequest()
->get($target_entity_type_id)) {
$transaction
->setTargetEntity($target_entity);
}
}
}
public function save(array $form, FormStateInterface $form_state) {
$transaction = $this->entity;
if ($transaction
->getType()
->getOption('execution') == TransactionTypeInterface::EXECUTION_ASK && $form_state
->getValue('execute', FALSE) && $transaction
->isPending()) {
$executed = $this->entity
->execute(FALSE);
}
$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));
if (isset($executed)) {
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;
}
}