You are here

class CurrencySign in Currency 8.3

Provides an element to configure a currency's sign.

Plugin annotation

@FormElement("currency_sign");

Hierarchy

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\Element
View 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

Namesort descending Modifiers Type Description Overrides
CurrencySign::$currencyStorage protected property The currency storage.
CurrencySign::create public static function Creates an instance of the plugin. Overrides ContainerFactoryPluginInterface::create
CurrencySign::CUSTOM_VALUE constant The value for the currency_sign form element's "custom" option.
CurrencySign::elementValidate public static function Implements form #element_validate callback.
CurrencySign::getInfo public function Returns the element properties for this element. Overrides ElementInterface::getInfo
CurrencySign::process public function Implements form #process callback.
CurrencySign::__construct public function Constructs a new instance. Overrides PluginBase::__construct
DependencySerializationTrait::$_entityStorages protected property An array of entity type IDs keyed by the property name of their storages.
DependencySerializationTrait::$_serviceIds protected property An array of service IDs keyed by property name used for serialization.
DependencySerializationTrait::__sleep public function 1
DependencySerializationTrait::__wakeup public function 2
FormElement::processAutocomplete public static function Adds autocomplete functionality to elements.
FormElement::processPattern public static function #process callback for #pattern form element property.
FormElement::validatePattern public static function #element_validate callback for #pattern form element property.
FormElement::valueCallback public static function Determines how user input is mapped to an element's #value property. Overrides FormElementInterface::valueCallback 15
FormElementCallbackTrait::__callStatic public static function Instantiates this class as a plugin and calls a method on it.
MessengerTrait::$messenger protected property The messenger. 29
MessengerTrait::messenger public function Gets the messenger. 29
MessengerTrait::setMessenger public function Sets the messenger.
PluginBase::$configuration protected property Configuration information passed into the plugin. 1
PluginBase::$pluginDefinition protected property The plugin implementation definition. 1
PluginBase::$pluginId protected property The plugin_id.
PluginBase::DERIVATIVE_SEPARATOR constant A string which is used to separate base plugin IDs from the derivative ID.
PluginBase::getBaseId public function Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface::getBaseId
PluginBase::getDerivativeId public function Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface::getDerivativeId
PluginBase::getPluginDefinition public function Gets the definition of the plugin implementation. Overrides PluginInspectionInterface::getPluginDefinition 3
PluginBase::getPluginId public function Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface::getPluginId
PluginBase::isConfigurable public function Determines if the plugin is configurable.
RenderElement::preRenderAjaxForm public static function Adds Ajax information about an element to communicate with JavaScript.
RenderElement::preRenderGroup public static function Adds members of this group as actual elements for rendering.
RenderElement::processAjaxForm public static function Form element processing handler for the #ajax form property. 1
RenderElement::processGroup public static function Arranges elements into groups.
RenderElement::setAttributes public static function Sets a form element's class attribute. Overrides ElementInterface::setAttributes
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.