You are here

class FormHelper in Currency 8.3

Provides form helpers.

Hierarchy

Expanded class hierarchy of FormHelper

1 file declares its use of FormHelper
FormHelperTest.php in tests/src/Unit/FormHelperTest.php
1 string reference to 'FormHelper'
currency.services.yml in ./currency.services.yml
currency.services.yml
1 service uses FormHelper
currency.form_helper in ./currency.services.yml
Drupal\currency\FormHelper

File

src/FormHelper.php, line 12

Namespace

Drupal\currency
View source
class FormHelper implements FormHelperInterface {
  use StringTranslationTrait;

  /**
   * The currency storage.
   *
   * @var \Drupal\Core\Entity\EntityStorageInterface
   */
  protected $currencyStorage;

  /**
   * The currency locale storage.
   *
   * @var \Drupal\Core\Entity\EntityStorageInterface
   */
  protected $currencyLocaleStorage;

  /**
   * Constructs a new instance.
   *
   * @param \Drupal\Core\StringTranslation\TranslationInterface
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   */
  public function __construct(TranslationInterface $string_translation, EntityTypeManagerInterface $entity_type_manager) {
    $this->currencyStorage = $entity_type_manager
      ->getStorage('currency');
    $this->currencyLocaleStorage = $entity_type_manager
      ->getStorage('currency_locale');
    $this->stringTranslation = $string_translation;
  }

  /**
   * {@inheritdoc}
   */
  public function getCurrencyOptions(array $currencies = NULL) {
    $options = array();

    /** @var \Drupal\currency\Entity\CurrencyInterface[] $currencies */
    if (is_null($currencies)) {
      $currencies = $this->currencyStorage
        ->loadMultiple();
    }
    foreach ($currencies as $currency) {

      // Do not show disabled currencies.
      if ($currency
        ->status()) {
        $options[$currency
          ->id()] = $this
          ->t('@currency_title (@currency_code)', array(
          '@currency_title' => $currency
            ->label(),
          '@currency_code' => $currency
            ->id(),
        ));
      }
    }
    natcasesort($options);
    return $options;
  }

  /**
   * {@inheritdoc}
   */
  public function getCurrencyLocaleOptions(array $currency_locales = NULL) {
    $options = array();

    /** @var \Drupal\currency\Entity\CurrencyLocaleInterface[] $currency_locales */
    if (is_null($currency_locales)) {
      $currency_locales = $this->currencyLocaleStorage
        ->loadMultiple();
    }
    foreach ($currency_locales as $currency_locale) {

      // Do not show disabled currency locales.
      if ($currency_locale
        ->status()) {
        $options[$currency_locale
          ->id()] = $currency_locale
          ->label();
      }
    }
    natcasesort($options);
    return $options;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
FormHelper::$currencyLocaleStorage protected property The currency locale storage.
FormHelper::$currencyStorage protected property The currency storage.
FormHelper::getCurrencyLocaleOptions public function Returns an options list of all currency locales. Overrides FormHelperInterface::getCurrencyLocaleOptions
FormHelper::getCurrencyOptions public function Returns an options list of all currencies. Overrides FormHelperInterface::getCurrencyOptions
FormHelper::__construct public function Constructs a new instance.
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.