View source
<?php
namespace Drupal\commerce_migrate_commerce\Plugin\migrate\source\commerce1;
use Drupal\commerce_price\CurrencyImporter;
use Drupal\migrate\Row;
use Drupal\migrate\Plugin\MigrationInterface;
use Drupal\migrate_drupal\Plugin\migrate\source\Variable;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Language\LanguageManagerInterface;
use Drupal\Core\State\StateInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class Currency extends Variable {
protected $languageManager;
protected $variables;
public function __construct(array $configuration, $plugin_id, $plugin_definition, MigrationInterface $migration, StateInterface $state, EntityTypeManagerInterface $entity_type_manager, LanguageManagerInterface $language_manager) {
parent::__construct($configuration, $plugin_id, $plugin_definition, $migration, $state, $entity_type_manager);
$this->languageManager = $language_manager;
$this->entityTypeManager = $entity_type_manager;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition, MigrationInterface $migration = NULL) {
return new static($configuration, $plugin_id, $plugin_definition, $migration, $container
->get('state'), $container
->get('entity_type.manager'), $container
->get('language_manager'));
}
public function fields() {
return parent::fields() + [
'currency_name' => $this
->t('Currency name'),
'numeric_code' => $this
->t('Currency code numeric code'),
];
}
public function prepareRow(Row $row) {
if (empty($row
->getSourceProperty('commerce_default_currency'))) {
$row
->setSourceProperty('commerce_default_currency', 'USD');
}
$currencyImporter = new CurrencyImporter($this->entityTypeManager, $this->languageManager);
$currency = $currencyImporter
->import($row
->getSourceProperty('commerce_default_currency'));
$name = $currency
->getName();
$numeric_code = $currency
->getNumericCode();
$row
->setSourceProperty('currency_name', $name);
$row
->setSourceProperty('numeric_code', $numeric_code);
return parent::prepareRow($row);
}
}