You are here

public function TransactionTypeFormBase::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/TransactionTypeFormBase.php, line 190

Class

TransactionTypeFormBase
Base form for transaction type forms.

Namespace

Drupal\transaction\Form

Code

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

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

  // Process options.
  $this
    ->saveOptions($form, $form_state);

  // Plugin needs that the transaction type is saved to create new fields.
  $status = $transaction_type
    ->isNew() ? $transaction_type
    ->save() : SAVED_UPDATED;

  // Set the transactor's config.
  $transaction_type
    ->getPlugin()
    ->submitConfigurationForm($form, $form_state);

  // Update the transaction type.
  $transaction_type
    ->save();

  // Messages.
  $t_args = [
    '%label' => $transaction_type
      ->label(),
    '@transactor' => $transaction_type
      ->getPluginId(),
    '@target' => $transaction_type
      ->getTargetEntityTypeId(),
  ];
  $logger_args = $t_args + [
    'link' => $transaction_type
      ->toLink($this
      ->t('Edit'), 'edit-form')
      ->toString(),
  ];
  if ($status == SAVED_UPDATED) {
    $this
      ->messenger()
      ->addStatus($this
      ->t('Transaction type %label has been updated.', $t_args));
    $this
      ->logger('transaction')
      ->notice('Transaction type %label has been updated.', $logger_args);
  }
  else {
    $this
      ->messenger()
      ->addStatus($this
      ->t('Transaction type %label has been added.', $t_args));
    $this
      ->logger('transaction')
      ->notice('New transaction type %label with transactor @transactor and target entity type @target has been added.', $logger_args);
  }
  $form_state
    ->setRedirectUrl($transaction_type
    ->toUrl('collection'));
}