class CurrencySign in Currency 8.3
Provides an element to configure a currency's sign.
Plugin annotation
@FormElement("currency_sign");
Hierarchy
- class \Drupal\Component\Plugin\PluginBase implements DerivativeInspectionInterface, PluginInspectionInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
- class \Drupal\Core\Render\Element\RenderElement implements ElementInterface
- class \Drupal\Core\Render\Element\FormElement implements FormElementInterface
- class \Drupal\currency\Element\CurrencySign implements ContainerFactoryPluginInterface uses FormElementCallbackTrait
- class \Drupal\Core\Render\Element\FormElement implements FormElementInterface
- class \Drupal\Core\Render\Element\RenderElement implements ElementInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
Expanded class hierarchy of CurrencySign
3 files declare their use of CurrencySign
- CurrencyFormWebTest.php in tests/
src/ Functional/ Entity/ Currency/ CurrencyFormWebTest.php - CurrencySignTest.php in tests/
src/ Unit/ Element/ CurrencySignTest.php - CurrencySignWebTest.php in tests/
src/ Functional/ Element/ CurrencySignWebTest.php
3 #type uses of CurrencySign
- CurrencyForm::form in src/
Entity/ Currency/ CurrencyForm.php - Gets the actual form array to be built.
- CurrencyFormTest::testForm in tests/
src/ Unit/ Entity/ Currency/ CurrencyFormTest.php - @covers ::form
- CurrencySignElement::buildForm in modules/
currency_test/ src/ CurrencySignElement.php - Form constructor.
File
- src/
Element/ CurrencySign.php, line 18
Namespace
Drupal\currency\ElementView source
class CurrencySign extends FormElement implements ContainerFactoryPluginInterface {
use FormElementCallbackTrait;
/**
* The value for the currency_sign form element's "custom" option.
*/
const CUSTOM_VALUE = '###CUSTOM###';
/**
* The currency storage.
*
* @var \Drupal\Core\Entity\EntityStorageInterface
*/
protected $currencyStorage;
/**
* Constructs a new instance.
*
* @param mixed[] $configuration
* A configuration array containing information about the plugin instance.
* @param string $plugin_id
* The plugin_id for the plugin instance.
* @param mixed $plugin_definition
* The plugin implementation definition.
* @param \Drupal\Core\Entity\EntityStorageInterface $currency_storage
*/
public function __construct(array $configuration, $plugin_id, array $plugin_definition, TranslationInterface $string_translation, EntityStorageInterface $currency_storage) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->currencyStorage = $currency_storage;
$this->stringTranslation = $string_translation;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
/** @var \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager */
$entity_type_manager = $container
->get('entity_type.manager');
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('string_translation'), $entity_type_manager
->getStorage('currency'));
}
/**
* {@inheritdoc}
*/
public function getInfo() {
$plugin_id = $this
->getPluginId();
return [
// The ISO 4217 code of the currency which signs to suggest to the user.
// Optional.
'#currency_code' => FALSE,
'#element_validate' => [
[
get_class($this),
'elementValidate',
],
],
'#process' => [
[
get_class($this),
'instantiate#process#' . $plugin_id,
],
],
];
}
/**
* Implements form #process callback.
*/
public function process(array $element, FormStateInterface $form_state, array $form) {
/** @var \Drupal\currency\Entity\CurrencyInterface $currency */
$currency = NULL;
if ($element['#currency_code']) {
$currency = $this->currencyStorage
->load($element['#currency_code']);
}
if (!$currency) {
$currency = $this->currencyStorage
->load('XXX');
}
// Modify the element.
$element['#tree'] = TRUE;
$element['#theme_wrappers'][] = 'form_element';
$element['#attached']['library'] = [
'currency/element.currency_sign',
];
$signs = array_merge(array(
$currency
->getSign(),
), $currency
->getAlternativeSigns());
$signs = array_combine($signs, $signs);
$signs = array_unique(array_filter(array_merge(array(
self::CUSTOM_VALUE => t('- Custom -'),
), $signs)));
asort($signs);
$element['sign'] = array(
'#default_value' => in_array($element['#default_value'], $signs) ? $element['#default_value'] : self::CUSTOM_VALUE,
'#empty_value' => '',
'#options' => $signs,
'#required' => $element['#required'],
'#title' => t('Sign'),
'#title_display' => 'invisible',
'#type' => 'select',
);
$sign_js_selector = '.form-type-currency-sign .form-select';
$element['sign_custom'] = array(
'#default_value' => $element['#default_value'],
'#states' => array(
'visible' => array(
$sign_js_selector => array(
'value' => self::CUSTOM_VALUE,
),
),
),
'#title' => t('Custom sign'),
'#title_display' => 'invisible',
'#type' => 'textfield',
);
return $element;
}
/**
* Implements form #element_validate callback.
*/
public static function elementValidate($element, FormStateInterface $form_state, $form) {
// Set a scalar value.
$sign = $element['#value']['sign'];
if ($sign == self::CUSTOM_VALUE) {
$sign = $element['#value']['sign_custom'];
}
$form_state
->setValueForElement($element, $sign);
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
CurrencySign:: |
protected | property | The currency storage. | |
CurrencySign:: |
public static | function |
Creates an instance of the plugin. Overrides ContainerFactoryPluginInterface:: |
|
CurrencySign:: |
constant | The value for the currency_sign form element's "custom" option. | ||
CurrencySign:: |
public static | function | Implements form #element_validate callback. | |
CurrencySign:: |
public | function |
Returns the element properties for this element. Overrides ElementInterface:: |
|
CurrencySign:: |
public | function | Implements form #process callback. | |
CurrencySign:: |
public | function |
Constructs a new instance. Overrides PluginBase:: |
|
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 | |
FormElement:: |
public static | function | Adds autocomplete functionality to elements. | |
FormElement:: |
public static | function | #process callback for #pattern form element property. | |
FormElement:: |
public static | function | #element_validate callback for #pattern form element property. | |
FormElement:: |
public static | function |
Determines how user input is mapped to an element's #value property. Overrides FormElementInterface:: |
15 |
FormElementCallbackTrait:: |
public static | function | Instantiates this class as a plugin and calls a method on it. | |
MessengerTrait:: |
protected | property | The messenger. | 29 |
MessengerTrait:: |
public | function | Gets the messenger. | 29 |
MessengerTrait:: |
public | function | Sets the messenger. | |
PluginBase:: |
protected | property | Configuration information passed into the plugin. | 1 |
PluginBase:: |
protected | property | The plugin implementation definition. | 1 |
PluginBase:: |
protected | property | The plugin_id. | |
PluginBase:: |
constant | A string which is used to separate base plugin IDs from the derivative ID. | ||
PluginBase:: |
public | function |
Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the definition of the plugin implementation. Overrides PluginInspectionInterface:: |
3 |
PluginBase:: |
public | function |
Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface:: |
|
PluginBase:: |
public | function | Determines if the plugin is configurable. | |
RenderElement:: |
public static | function | Adds Ajax information about an element to communicate with JavaScript. | |
RenderElement:: |
public static | function | Adds members of this group as actual elements for rendering. | |
RenderElement:: |
public static | function | Form element processing handler for the #ajax form property. | 1 |
RenderElement:: |
public static | function | Arranges elements into groups. | |
RenderElement:: |
public static | function |
Sets a form element's class attribute. Overrides ElementInterface:: |
|
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. |