You are here

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

Class

CommerceCurrencyResolverMapping
Class CommerceCurrencyResolverMapping.

Namespace

Drupal\commerce_currency_resolver\Form

Code

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

  // Get type of mapping for the resolver.
  $currency_mapping = $this
    ->config('commerce_currency_resolver.settings')
    ->get('currency_mapping');

  // Get default currency.
  $currency_default = $this
    ->config('commerce_currency_resolver.settings')
    ->get('currency_default');

  // Get current settings.
  $config = $this
    ->config('commerce_currency_resolver.currency_mapping');

  // Get active currencies.
  $active_currencies = $this->currencyHelper
    ->getCurrencies();

  // Get mapped currency.
  $matrix = $config
    ->get('matrix');
  switch ($currency_mapping) {
    case 'lang':
      $languages = $this->currencyHelper
        ->getLanguages();
      $form['matrix'] = [
        '#type' => 'details',
        '#open' => TRUE,
        '#title' => $this
          ->t('Currency matrix'),
        '#tree' => TRUE,
        '#weight' => 50,
      ];
      foreach ($languages as $key => $language) {
        $form['matrix'][$key] = [
          '#type' => 'radios',
          '#options' => $active_currencies,
          '#default_value' => $matrix[$key],
          '#title' => $language,
          '#description' => $this
            ->t('Select currency which should be used with @lang language', [
            '@lang' => $language,
          ]),
        ];
      }
      break;
    case 'geo':
      $domicile_currency = $config
        ->get('domicile_currency');
      $logic = !empty($config
        ->get('logic')) ? $config
        ->get('logic') : 'country';
      $form['domicile_currency'] = [
        '#type' => 'checkbox',
        '#default_value' => $domicile_currency,
        '#title' => $this
          ->t('Use domicile currency per country.'),
        '#description' => $this
          ->t('If domicile currency is enabled for specific country, it will be considered as primary currency. Otherwise currency resolver use default currency defined in settings as fallback.'),
      ];

      // Use mapping per country.
      if (empty($domicile_currency)) {
        $form['logic'] = [
          '#type' => 'select',
          '#default_value' => $logic,
          '#options' => [
            'country' => $this
              ->t('Country'),
            'currency' => $this
              ->t('Currency'),
          ],
          '#title' => $this
            ->t('Matrix logic'),
          '#description' => $this
            ->t('How you want to create matrix. You can assign currency to each country separate, or assign multiple countries to each currency'),
        ];
        $form['matrix'] = [
          '#type' => 'details',
          '#open' => FALSE,
          '#title' => $this
            ->t('Currency matrix'),
          '#tree' => TRUE,
          '#weight' => 50,
        ];

        // Country list. Get country list which is already processed with
        // alters instead of taking static list.
        $countries = $this->countryManager
          ->getList();
        switch ($logic) {
          default:
          case 'country':
            foreach ($countries as $key => $value) {
              $form['matrix'][$key] = [
                '#type' => 'select',
                '#options' => $active_currencies,
                '#title' => $value
                  ->render(),
                '#description' => $this
                  ->t('Select currency which should be used with @lang language', [
                  '@lang' => $value
                    ->render(),
                ]),
                '#default_value' => $matrix[$key] ?? $currency_default,
              ];
            }
            break;
          case 'currency':
            $data = [];

            // Process and reverse existing config from country->currency
            // to currency -> countries list for autocomplete fields.
            if (!empty($matrix)) {
              foreach ($matrix as $country => $currency) {
                $data[$currency] .= $country;
              }
            }

            // Render autocomplete fields for each currency.
            foreach ($active_currencies as $key => $currency) {
              $form['matrix'][$key] = [
                '#type' => 'textfield',
                '#autocomplete_route_name' => 'commerce_currency_resolver.countries.autocomplete',
                '#title' => $currency,
                '#description' => $this
                  ->t('Select countires which should be used with @currency currency', [
                  '@currency' => $currency,
                ]),
                '#default_value' => isset($data[$key]) ? str_replace(' ', ', ', $data[$key]) : '',
              ];
            }
            break;
        }
      }
      break;
  }
  $form['submit'] = [
    '#type' => 'submit',
    '#value' => $this
      ->t('Submit'),
    '#weight' => 100,
  ];
  return $form;
}