View source
<?php
namespace Drupal\currency\Entity\CurrencyLocale;
use Drupal\Core\Entity\EntityForm;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Language\LanguageManager;
use Drupal\Core\Locale\CountryManagerInterface;
use Drupal\Core\StringTranslation\TranslationInterface;
use Drupal\Core\Utility\LinkGeneratorInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class CurrencyLocaleForm extends EntityForm {
protected $countryManager;
protected $currencyLocaleStorage;
protected $linkGenerator;
public function __construct(TranslationInterface $string_translation, LinkGeneratorInterface $link_generator, EntityStorageInterface $currency_locale_storage, CountryManagerInterface $country_manager) {
$this->countryManager = $country_manager;
$this->currencyLocaleStorage = $currency_locale_storage;
$this->linkGenerator = $link_generator;
$this->stringTranslation = $string_translation;
}
public static function create(ContainerInterface $container) {
$entity_type_manager = $container
->get('entity_type.manager');
return new static($container
->get('string_translation'), $container
->get('link_generator'), $entity_type_manager
->getStorage('currency_locale'), $container
->get('country_manager'));
}
protected function copyFormValuesToEntity(EntityInterface $entity, array $form, FormStateInterface $form_state) {
$currency_locale = $entity;
$values = $form_state
->getValues();
$currency_locale
->setLocale($values['language_code'], $values['country_code']);
$currency_locale
->setPattern($values['pattern']);
$currency_locale
->setDecimalSeparator($values['decimal_separator']);
$currency_locale
->setGroupingSeparator($values['grouping_separator']);
}
public function form(array $form, FormStateInterface $form_state) {
$currency_locale = $this
->getEntity();
$options = array();
foreach (LanguageManager::getStandardLanguageList() as $language_code => $language_names) {
$options[$language_code] = $language_names[0];
}
natcasesort($options);
$form['language_code'] = array(
'#default_value' => $currency_locale
->getLanguageCode(),
'#empty_value' => '',
'#options' => $options,
'#required' => TRUE,
'#title' => $this
->t('Language'),
'#type' => 'select',
);
$form['country_code'] = array(
'#default_value' => $currency_locale
->getCountryCode(),
'#empty_value' => '',
'#options' => $this->countryManager
->getList(),
'#required' => TRUE,
'#title' => $this
->t('Country'),
'#type' => 'select',
);
$form['formatting'] = array(
'#open' => TRUE,
'#title' => $this
->t('Formatting'),
'#type' => 'details',
);
$form['formatting']['decimal_separator'] = array(
'#default_value' => $currency_locale
->getDecimalSeparator(),
'#maxlength' => 255,
'#required' => TRUE,
'#size' => 3,
'#title' => $this
->t('Decimal separator'),
'#type' => 'textfield',
);
$form['formatting']['grouping_separator'] = array(
'#default_value' => $currency_locale
->getGroupingSeparator(),
'#maxlength' => 255,
'#size' => 3,
'#title' => $this
->t('Group separator'),
'#type' => 'textfield',
);
$form['formatting']['pattern'] = array(
'#default_value' => $currency_locale
->getPattern(),
'#description' => $this
->t('A Unicode <abbr title="Common Locale Data Repository">CLDR</abbr> <a href="http://cldr.unicode.org/translation/number-patterns">currency number pattern</a>.'),
'#maxlength' => 255,
'#title' => $this
->t('Pattern'),
'#type' => 'textfield',
);
return parent::form($form, $form_state, $currency_locale);
}
public function validateForm(array &$form, FormStateInterface $form_state) {
parent::validateForm($form, $form_state);
$currency_locale = $this
->getEntity();
if ($currency_locale
->isNew()) {
$values = $form_state
->getValues();
$locale = strtolower($values['language_code']) . '_' . strtoupper($values['country_code']);
$loaded_currency_locale = $this->currencyLocaleStorage
->load($locale);
if ($loaded_currency_locale) {
$form_state
->setError($form['locale'], $this
->t('A pattern for this locale already exists.'));
}
}
}
public function save(array $form, FormStateInterface $form_state) {
$currency_locale = $this
->getEntity($form_state);
$currency_locale
->save();
$this
->messenger()
->addMessage($this
->t('The currency locale %label has been saved.', array(
'%label' => $currency_locale
->label(),
)));
$form_state
->setRedirect('entity.currency_locale.collection');
}
}