You are here

protected function Sermepa::getAvailableCurrencies in Commerce sermepa 8.2

Returns only the active currencies.

We don't want allow to configure a currency which is not active in our site.

Return value

array The available currencies array values.

Throws

\Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException

\Drupal\Component\Plugin\Exception\PluginNotFoundException

1 call to Sermepa::getAvailableCurrencies()
Sermepa::buildConfigurationForm in src/Plugin/Commerce/PaymentGateway/Sermepa.php
Form constructor.

File

src/Plugin/Commerce/PaymentGateway/Sermepa.php, line 433

Class

Sermepa
Provides the Sermepa/Redsýs payment gateway.

Namespace

Drupal\commerce_sermepa\Plugin\Commerce\PaymentGateway

Code

protected function getAvailableCurrencies() {

  // Get the supported currencies.
  $sermepa_currencies = SermepaApi::getAvailableCurrencies();
  $currency_storage = $this->entityTypeManager
    ->getStorage('commerce_currency');

  // Use the supported currencies to load only the enable currencies.
  $currency_ids = $currency_storage
    ->getQuery()
    ->condition('numericCode', array_keys($sermepa_currencies), 'IN')
    ->execute();
  $available_currencies = [];
  if ($currency_ids) {

    /** @var \Drupal\commerce_price\Entity\CurrencyInterface[] $enabled_currencies */
    $enabled_currencies = $currency_storage
      ->loadMultiple($currency_ids);

    // Prepare the currency array to use in the form element.
    foreach ($enabled_currencies as $currency) {
      $available_currencies[$currency
        ->getNumericCode()] = $currency
        ->getName();
    }
  }
  return $available_currencies;
}