CurrencyImportForm.php in Currency 8.3
File
src/Form/CurrencyImportForm.php
View source
<?php
namespace Drupal\currency\Form;
use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Render\Element\Form;
use Drupal\Core\StringTranslation\TranslationInterface;
use Drupal\Core\Url;
use Drupal\currency\ConfigImporterInterface;
use Drupal\currency\FormHelperInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class CurrencyImportForm extends FormBase implements ContainerInjectionInterface {
protected $configImporter;
protected $formHelper;
public function __construct(TranslationInterface $string_translation, ConfigImporterInterface $config_importer, FormHelperInterface $form_helper) {
$this->configImporter = $config_importer;
$this->formHelper = $form_helper;
$this->stringTranslation = $string_translation;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('string_translation'), $container
->get('currency.config_importer'), $container
->get('currency.form_helper'));
}
public function getFormId() {
return 'currency_import';
}
public function buildForm(array $form, FormStateInterface $form_state) {
$currencies = $this->configImporter
->getImportableCurrencies();
if (empty($currencies)) {
$form['message'] = [
'#markup' => $this
->t('All currencies have been imported already.'),
];
}
else {
$form['currency_code'] = [
'#options' => $this->formHelper
->getCurrencyOptions($currencies),
'#required' => TRUE,
'#title' => $this
->t('Currency'),
'#type' => 'select',
];
$form['actions'] = [
'#type' => 'actions',
];
$form['actions']['import'] = [
'#dropbutton' => 'submit',
'#name' => 'import',
'#type' => 'submit',
'#value' => $this
->t('Import'),
];
$form['actions']['import_edit'] = [
'#dropbutton' => 'submit',
'#name' => 'import_edit',
'#type' => 'submit',
'#value' => $this
->t('Import and edit'),
];
}
return $form;
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$currency = $this->configImporter
->importCurrency($form_state
->getValues()['currency_code']);
$this
->messenger()
->addMessage($this
->t('The %label has been imported.', [
'%label' => $currency
->label(),
]));
if ($form_state
->getTriggeringElement()['#name'] == 'import_edit') {
$form_state
->setRedirectUrl($currency
->toUrl('edit-form'));
}
else {
$form_state
->setRedirectUrl(new Url('entity.currency.collection'));
}
}
}