You are here

public function CommerceCurrencyResolverForm::buildForm in Commerce Currency Resolver 8

Form constructor.

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

array The form structure.

Overrides ConfigFormBase::buildForm

File

src/Form/CommerceCurrencyResolverForm.php, line 65

Class

CommerceCurrencyResolverForm
Class CommerceCurrencyResolverForm.

Namespace

Drupal\commerce_currency_resolver\Form

Code

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

  // Get current settings.
  $config = $this
    ->config('commerce_currency_resolver.settings');
  $form['currency_mapping'] = [
    '#type' => 'radios',
    '#title' => $this
      ->t('Currency mapping'),
    '#description' => $this
      ->t('Select how currency is mapped in the system. By country, language.'),
    '#options' => $this
      ->getAvailableMapping(),
    '#default_value' => $config
      ->get('currency_mapping'),
    '#required' => TRUE,
  ];
  $geo_modules = $this->currencyHelper
    ->getGeoModules();
  if (!empty($geo_modules) && $config
    ->get('currency_mapping') === 'geo') {
    $form['currency_geo'] = [
      '#type' => 'radios',
      '#title' => $this
        ->t('Location module'),
      '#description' => $this
        ->t('Select which module is used for geolocation services'),
      '#options' => $geo_modules,
      '#default_value' => $config
        ->get('currency_geo'),
      '#required' => TRUE,
    ];
  }
  $form['currency_source'] = [
    '#type' => 'radios',
    '#title' => $this
      ->t('Currency source'),
    '#description' => $this
      ->t('Select how currency is added and calculated. To avoid possible errors, "Combo mode" is best option. If field for currency does not exist, it will fallback to automatic conversion for this specific currency'),
    '#options' => [
      'auto' => $this
        ->t('Automatic conversion'),
      'field' => $this
        ->t('Price field per currency'),
      'combo' => $this
        ->t('Combo mode'),
    ],
    '#default_value' => $config
      ->get('currency_source'),
    '#required' => TRUE,
  ];
  $exchange_rates = $this->currencyHelper
    ->getExchangeRatesProviders();
  $form['currency_exchange_rates'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Exchange rate API'),
    '#description' => $this
      ->t('Select which external service you want to use for calculating exchange rates between currencies'),
    '#options' => $exchange_rates,
    '#default_value' => $config
      ->get('currency_exchange_rates'),
    '#required' => TRUE,
  ];

  // If there is no exchanger plugin.
  if (empty($exchange_rates)) {
    $form['currency_exchange_rates']['#field_suffix'] = $this
      ->t('Please add at least one exchange rate provider under Exchange rates');
    $form['submit']['#disabled'] = TRUE;
  }
  $form['currency_default'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Default currency'),
    '#description' => $this
      ->t('Select currency which you consider default - as fallback if all resolvers fails, and upon exchange rates should be calculated'),
    '#options' => $this->currencyHelper
      ->getCurrencies(),
    '#default_value' => $config
      ->get('currency_default'),
    '#required' => TRUE,
  ];
  $form['submit'] = [
    '#type' => 'submit',
    '#value' => $this
      ->t('Submit'),
  ];
  return $form;
}