class CurrencyLocaleForm in Currency 8.3
Provides a currency_locale add/edit form.
Hierarchy
- class \Drupal\Core\Form\FormBase implements ContainerInjectionInterface, FormInterface uses DependencySerializationTrait, LoggerChannelTrait, MessengerTrait, LinkGeneratorTrait, RedirectDestinationTrait, UrlGeneratorTrait, StringTranslationTrait
- class \Drupal\Core\Entity\EntityForm implements EntityFormInterface
- class \Drupal\currency\Entity\CurrencyLocale\CurrencyLocaleForm
- class \Drupal\Core\Entity\EntityForm implements EntityFormInterface
Expanded class hierarchy of CurrencyLocaleForm
1 file declares its use of CurrencyLocaleForm
- CurrencyLocaleFormTest.php in tests/
src/ Unit/ Entity/ CurrencyLocale/ CurrencyLocaleFormTest.php
File
- src/
Entity/ CurrencyLocale/ CurrencyLocaleForm.php, line 18
Namespace
Drupal\currency\Entity\CurrencyLocaleView source
class CurrencyLocaleForm extends EntityForm {
/**
* The country manager.
*
* @var \Drupal\Core\Locale\CountryManagerInterface
*/
protected $countryManager;
/**
* The currency locale storage.
*
* @var \Drupal\Core\Entity\EntityStorageInterface
*/
protected $currencyLocaleStorage;
/**
* The link generator.
*
* @var \Drupal\Core\Utility\LinkGeneratorInterface
*/
protected $linkGenerator;
/**
* Constructs a new instance.
*
* @param \Drupal\Core\StringTranslation\TranslationInterface $string_translation
* The string translator.
* @param \Drupal\Core\Utility\LinkGeneratorInterface $link_generator
* The link generator.
* @param \Drupal\Core\Entity\EntityStorageInterface $currency_locale_storage
* The currency storage.
* @param \Drupal\Core\Locale\CountryManagerInterface $country_manager
* The country manager.
*/
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;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
/** @var \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager */
$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'));
}
/**
* {@inheritdoc}
*/
protected function copyFormValuesToEntity(EntityInterface $entity, array $form, FormStateInterface $form_state) {
/** @var \Drupal\currency\Entity\CurrencyLocaleInterface $currency_locale */
$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']);
}
/**
* {@inheritdoc}
*/
public function form(array $form, FormStateInterface $form_state) {
/** @var \Drupal\currency\Entity\CurrencyLocaleInterface $currency_locale */
$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);
}
/**
* {@inheritdoc}
*/
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.'));
}
}
}
/**
* {@inheritdoc}.
*/
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');
}
}
Members
Name![]() |
Modifiers | Type | Description | Overrides |
---|---|---|---|---|
CurrencyLocaleForm:: |
protected | property | The country manager. | |
CurrencyLocaleForm:: |
protected | property | The currency locale storage. | |
CurrencyLocaleForm:: |
protected | property |
The link generator. Overrides LinkGeneratorTrait:: |
|
CurrencyLocaleForm:: |
protected | function |
Copies top-level form values to entity properties Overrides EntityForm:: |
|
CurrencyLocaleForm:: |
public static | function |
Instantiates a new instance of this class. Overrides FormBase:: |
|
CurrencyLocaleForm:: |
public | function |
Gets the actual form array to be built. Overrides EntityForm:: |
|
CurrencyLocaleForm:: |
public | function |
. Overrides EntityForm:: |
|
CurrencyLocaleForm:: |
public | function |
Form validation handler. Overrides FormBase:: |
|
CurrencyLocaleForm:: |
public | function | Constructs a new instance. | |
DependencySerializationTrait:: |
protected | property | An array of entity type IDs keyed by the property name of their storages. | |
DependencySerializationTrait:: |
protected | property | An array of service IDs keyed by property name used for serialization. | |
DependencySerializationTrait:: |
public | function | 1 | |
DependencySerializationTrait:: |
public | function | 2 | |
EntityForm:: |
protected | property | The entity being used by this form. | 7 |
EntityForm:: |
protected | property | The entity type manager. | 3 |
EntityForm:: |
protected | property | The module handler service. | |
EntityForm:: |
protected | property | The name of the current operation. | |
EntityForm:: |
private | property | The entity manager. | |
EntityForm:: |
protected | function | Returns an array of supported actions for the current entity form. | 29 |
EntityForm:: |
protected | function | Returns the action form element for the current entity form. | |
EntityForm:: |
public | function | Form element #after_build callback: Updates the entity with submitted data. | |
EntityForm:: |
public | function |
Builds an updated entity object based upon the submitted form values. Overrides EntityFormInterface:: |
2 |
EntityForm:: |
public | function |
Form constructor. Overrides FormInterface:: |
10 |
EntityForm:: |
public | function |
Returns a string identifying the base form. Overrides BaseFormIdInterface:: |
5 |
EntityForm:: |
public | function |
Gets the form entity. Overrides EntityFormInterface:: |
|
EntityForm:: |
public | function |
Determines which entity will be used by this form from a RouteMatch object. Overrides EntityFormInterface:: |
1 |
EntityForm:: |
public | function |
Returns a unique string identifying the form. Overrides FormInterface:: |
10 |
EntityForm:: |
public | function |
Gets the operation identifying the form. Overrides EntityFormInterface:: |
|
EntityForm:: |
protected | function | Initialize the form state and the entity before the first form build. | 3 |
EntityForm:: |
protected | function | Prepares the entity object before the form is built first. | 3 |
EntityForm:: |
protected | function | Invokes the specified prepare hook variant. | |
EntityForm:: |
public | function | Process callback: assigns weights and hides extra fields. | |
EntityForm:: |
public | function |
Sets the form entity. Overrides EntityFormInterface:: |
|
EntityForm:: |
public | function |
Sets the entity manager for this form. Overrides EntityFormInterface:: |
|
EntityForm:: |
public | function |
Sets the entity type manager for this form. Overrides EntityFormInterface:: |
|
EntityForm:: |
public | function |
Sets the module handler for this form. Overrides EntityFormInterface:: |
|
EntityForm:: |
public | function |
Sets the operation for this form. Overrides EntityFormInterface:: |
|
EntityForm:: |
public | function |
This is the default entity object builder function. It is called before any
other submit handler to build the new entity object to be used by the
following submit handlers. At this point of the form workflow the entity is
validated and the form state… Overrides FormInterface:: |
17 |
EntityForm:: |
public | function | ||
EntityForm:: |
public | function | ||
FormBase:: |
protected | property | The config factory. | 1 |
FormBase:: |
protected | property | The request stack. | 1 |
FormBase:: |
protected | property | The route match. | |
FormBase:: |
protected | function | Retrieves a configuration object. | |
FormBase:: |
protected | function | Gets the config factory for this form. | 1 |
FormBase:: |
private | function | Returns the service container. | |
FormBase:: |
protected | function | Gets the current user. | |
FormBase:: |
protected | function | Gets the request object. | |
FormBase:: |
protected | function | Gets the route match. | |
FormBase:: |
protected | function | Gets the logger for a specific channel. | |
FormBase:: |
protected | function |
Returns a redirect response object for the specified route. Overrides UrlGeneratorTrait:: |
|
FormBase:: |
public | function | Resets the configuration factory. | |
FormBase:: |
public | function | Sets the config factory for this form. | |
FormBase:: |
public | function | Sets the request stack object to use. | |
LinkGeneratorTrait:: |
protected | function | Returns the link generator. | |
LinkGeneratorTrait:: |
protected | function | Renders a link to a route given a route name and its parameters. | |
LinkGeneratorTrait:: |
public | function | Sets the link generator service. | |
LoggerChannelTrait:: |
protected | property | The logger channel factory service. | |
LoggerChannelTrait:: |
protected | function | Gets the logger for a specific channel. | |
LoggerChannelTrait:: |
public | function | Injects the logger channel factory. | |
MessengerTrait:: |
protected | property | The messenger. | 29 |
MessengerTrait:: |
public | function | Gets the messenger. | 29 |
MessengerTrait:: |
public | function | Sets the messenger. | |
RedirectDestinationTrait:: |
protected | property | The redirect destination service. | 1 |
RedirectDestinationTrait:: |
protected | function | Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url. | |
RedirectDestinationTrait:: |
protected | function | Returns the redirect destination service. | |
RedirectDestinationTrait:: |
public | function | Sets the redirect destination service. | |
StringTranslationTrait:: |
protected | property | The string translation service. | 1 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. | |
UrlGeneratorTrait:: |
protected | property | The url generator. | |
UrlGeneratorTrait:: |
protected | function | Returns the URL generator service. | |
UrlGeneratorTrait:: |
public | function | Sets the URL generator service. | |
UrlGeneratorTrait:: |
protected | function | Generates a URL or path for a specific route based on the given parameters. |