CurrencyImportForm.php in Commerce Core 8.2
File
modules/price/src/Form/CurrencyImportForm.php
View source
<?php
namespace Drupal\commerce_price\Form;
use Drupal\commerce_price\CurrencyImporterInterface;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class CurrencyImportForm extends FormBase {
protected $importer;
public function __construct(CurrencyImporterInterface $importer) {
$this->importer = $importer;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('commerce_price.currency_importer'));
}
public function getFormId() {
return 'commerce_price_currency_import';
}
public function buildForm(array $form, FormStateInterface $form_state) {
$currencies = $this->importer
->getImportable();
if (empty($currencies)) {
$form['message'] = [
'#markup' => $this
->t('All currencies have already been imported.'),
];
}
else {
$form['currency_codes'] = [
'#type' => 'select',
'#title' => $this
->t('Available currencies'),
'#options' => $currencies,
'#multiple' => TRUE,
'#size' => 10,
];
$form['actions']['#type'] = 'actions';
$form['actions']['import'] = [
'#type' => 'submit',
'#button_type' => 'primary',
'#name' => 'import',
'#value' => $this
->t('Add'),
'#submit' => [
'::submitForm',
],
];
}
return $form;
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$currency_codes = $form_state
->getValue('currency_codes');
foreach ($currency_codes as $currency_code) {
$this->importer
->import($currency_code);
}
$this
->messenger()
->addMessage($this
->t('Imported the selected currencies.'));
$form_state
->setRedirect('entity.commerce_currency.collection');
}
}