Skip to content
Snippets Groups Projects

Issue #3493300 by zaporylie: Import currency with a config action

1 file
+ 67
0
Compare changes
  • Side-by-side
  • Inline
<?php
declare(strict_types=1);
namespace Drupal\commerce_price\Plugin\ConfigAction;
use Drupal\commerce_price\CurrencyImporterInterface;
use Drupal\Core\Config\Action\Attribute\ConfigAction;
use Drupal\Core\Config\Action\ConfigActionPluginInterface;
use Drupal\Core\Config\Action\Exists;
use Drupal\Core\Config\ConfigManagerInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* @internal
* This API is experimental.
*/
#[ConfigAction(
id: 'currencyImport',
admin_label: new TranslatableMarkup('Import currency from repository'),
entity_types: ['commerce_currency'],
)]
final class CurrencyImport implements ConfigActionPluginInterface, ContainerFactoryPluginInterface {
/**
* Constructs a CurrencyImport object.
*
* @param \Drupal\Core\Config\ConfigManagerInterface $configManager
* The config manager.
* @param \Drupal\commerce_price\CurrencyImporterInterface $currencyImporter
* The currency importer.
* @param \Drupal\Core\Config\Action\Exists $exists
* Determines behavior of action depending on entity existence.
*/
public function __construct(
protected readonly ConfigManagerInterface $configManager,
protected readonly CurrencyImporterInterface $currencyImporter,
protected readonly Exists $exists,
) {
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition): static {
return new static(
$container->get('config.manager'),
$container->get('commerce_price.currency_importer'),
Exists::ReturnEarlyIfExists,
);
}
/**
* {@inheritdoc}
*/
public function apply(string $configName, mixed $value): void {
/** @var \Drupal\Core\Config\Entity\ConfigEntityInterface|null $entity */
$entity = $this->configManager->loadConfigEntityByName(str_replace('XYZ', $value, $configName));
if ($this->exists->returnEarly($configName, $entity)) {
return;
}
$this->currencyImporter->import($value);
}
}
Loading