Add functionality to import all currencies at once

Created on 31 May 2024, 4 months ago

Hello,

I identified a need for the ability to import all available currencies simultaneously within the Currency module, as the current UI only supports individual currency importation. This feature would be particularly useful for users who deal with multiple currencies, saving them time and effort.

Unfortunately, I'm unable to contribute code at the moment, but I've conceptualized a Drush command that could achieve this bulk import of currencies. If anyone is able to take this on, I believe it would be a valuable addition to the module.

The command imports all currencies that have not been imported yet. Here is the code snippet for the command:


namespace Drupal\payment_invitation\Drush\Commands;

use Drupal\currency\ConfigImporterInterface;
use Drush\Attributes as CLI;
use Drush\Commands\DrushCommands;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
 * A Drush commandfile.
 */
class PaymentInvitationCommands extends DrushCommands {

  /**
   * Constructs a PaymentInvitationCommands object.
   */
  public function __construct(
    protected readonly ConfigImporterInterface $currencyConfigImporter,
  ) {
    parent::__construct();
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static(
      $container->get('currency.config_importer'),
    );
  }

  /**
   * Imports all available currencies.
   *
   * @command payment_invitation:import-currencies
   * @aliases pic
   * @usage payment_invitation:import-currencies
   *   Import all currencies that have not been imported yet.
   */
  #[CLI\Command(name: 'payment_invitation:import-currencies', aliases: ['pic'])]
  public function importCurrencies() {
    $importableCurrencies = $this->currencyConfigImporter->getImportableCurrencies();
    if (empty($importableCurrencies)) {
      $this->logger()->notice(dt('All currencies have been imported already.'));
    } else {
      foreach ($importableCurrencies as $currency_code => $currency_name) {
        try {
          $currency = $this->currencyConfigImporter->importCurrency($currency_code);
          if ($currency) {
            $this->logger()->success(dt('The %label has been imported.', [
              '%label' => $currency->label(),
            ]));
          } else {
            $this->logger()->error(dt('There was an error importing %label.', [
              '%label' => $currency_name,
            ]));
          }
        }
        catch (\Exception $e) {
          $this->logger()->error(dt('An unexpected error occurred while importing %label: @error', [
            '%label' => $currency_name,
            '@error' => $e->getMessage(),
          ]));
        }
      }
    }
  }

}

This feature would streamline the process for users who need to work with multiple currencies and enhance the overall functionality of the Currency module.

Thank you.

Feature request
Status

Active

Version

3.0

Component

Code

Created by

🇪🇸Spain psf_ Huelva

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Production build 0.71.5 2024