You are here

public function FixedRatesForm::submitForm in Currency 8.3

Form submission handler.

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 FormInterface::submitForm

File

src/Form/FixedRatesForm.php, line 149

Class

FixedRatesForm
Provides the configuration form for the currency_fixed_rates plugin.

Namespace

Drupal\currency\Form

Code

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

  /** @var \Drupal\currency\Plugin\Currency\ExchangeRateProvider\FixedRates $plugin */
  $plugin = $this->currencyExchangeRateProviderManager
    ->createInstance('currency_fixed_rates');
  $values = $form_state
    ->getValues();
  $currency_code_from = $values['currency_code_from'];
  $currency_code_to = $values['currency_code_to'];
  $currency_from = $this->currencyStorage
    ->load($currency_code_from);
  $currency_to = $this->currencyStorage
    ->load($currency_code_to);
  $triggering_element = $form_state
    ->getTriggeringElement();
  switch ($triggering_element['#name']) {
    case 'save':
      $plugin
        ->save($currency_code_from, $currency_code_to, $values['rate']['amount']);
      $this
        ->messenger()
        ->addMessage($this
        ->t('The exchange rate for @currency_title_from to @currency_title_to has been saved.', array(
        '@currency_title_from' => $currency_from
          ->label(),
        '@currency_title_to' => $currency_to
          ->label(),
      )));
      break;
    case 'delete':
      $plugin
        ->delete($currency_code_from, $currency_code_to);
      $this
        ->messenger()
        ->addMessage($this
        ->t('The exchange rate for @currency_title_from to @currency_title_to has been deleted.', array(
        '@currency_title_from' => $currency_from
          ->label(),
        '@currency_title_to' => $currency_to
          ->label(),
      )));
      break;
  }
  $form_state
    ->setRedirect('currency.exchange_rate_provider.fixed_rates.overview');
}