View source
<?php
namespace Drupal\commerce_currency_resolver\Form;
use Drupal\commerce_currency_resolver\CurrencyHelperInterface;
use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Locale\CountryManagerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\Config\ConfigFactoryInterface;
class CommerceCurrencyResolverMapping extends ConfigFormBase {
protected $countryManager;
protected $currencyHelper;
public function __construct(ConfigFactoryInterface $config_factory, CountryManagerInterface $country_manager, CurrencyHelperInterface $currencyHelper) {
parent::__construct($config_factory);
$this->countryManager = $country_manager;
$this->currencyHelper = $currencyHelper;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('config.factory'), $container
->get('country_manager'), $container
->get('commerce_currency_resolver.currency_helper'));
}
public function getFormId() {
return 'commerce_currency_resolver_admin_mapping';
}
protected function getEditableConfigNames() {
return [
'commerce_currency_resolver.currency_mapping',
];
}
public function buildForm(array $form, FormStateInterface $form_state) {
$currency_mapping = $this
->config('commerce_currency_resolver.settings')
->get('currency_mapping');
$currency_default = $this
->config('commerce_currency_resolver.settings')
->get('currency_default');
$config = $this
->config('commerce_currency_resolver.currency_mapping');
$active_currencies = $this->currencyHelper
->getCurrencies();
$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.'),
];
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,
];
$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 = [];
if (!empty($matrix)) {
foreach ($matrix as $country => $currency) {
$data[$currency] .= $country;
}
}
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;
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$config = $this
->config('commerce_currency_resolver.currency_mapping');
$logic = $form_state
->getValue('logic');
if ($logic === 'currency') {
$raw_data = $form_state
->getValue('matrix');
$matrix = [];
foreach ($raw_data as $currency => $list) {
$countries = explode(',', $list);
foreach ($countries as $country) {
$matrix[$country] = $currency;
}
}
}
else {
$matrix = $form_state
->getValue('matrix');
}
$config
->set('domicile_currency', $form_state
->getValue('domicile_currency'))
->set('logic', $logic)
->set('matrix', $matrix)
->save();
parent::submitForm($form, $form_state);
}
}