ProductVariationDeriver.php in Commerce Migrate 3.1.x
File
modules/commerce/src/Plugin/migrate/ProductVariationDeriver.php
View source
<?php
namespace Drupal\commerce_migrate_commerce\Plugin\migrate;
use Drupal\Component\Plugin\Derivative\DeriverBase;
use Drupal\Core\Database\DatabaseExceptionWrapper;
use Drupal\Core\Plugin\Discovery\ContainerDeriverInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\migrate\Exception\RequirementsException;
use Drupal\migrate\Plugin\MigrationDeriverTrait;
use Drupal\migrate_drupal\FieldDiscoveryInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class ProductVariationDeriver extends DeriverBase implements ContainerDeriverInterface {
use MigrationDeriverTrait;
use StringTranslationTrait;
protected $basePluginId;
protected $includeTranslations;
protected $fieldDiscovery;
public function __construct($base_plugin_id, $translations, FieldDiscoveryInterface $field_discovery) {
$this->basePluginId = $base_plugin_id;
$this->includeTranslations = $translations;
$this->fieldDiscovery = $field_discovery;
}
public static function create(ContainerInterface $container, $base_plugin_id) {
return new static($base_plugin_id, $container
->get('module_handler')
->moduleExists('content_translation'), $container
->get('migrate_drupal.field_discovery'));
}
public function getDerivativeDefinitions($base_plugin_definition) {
$product_variation_types = static::getSourcePlugin('commerce1_product_type');
try {
$product_variation_types
->checkRequirements();
} catch (RequirementsException $e) {
return $this->derivatives;
}
$fields = [];
try {
$source_plugin = static::getSourcePlugin('d7_field_instance');
$source_plugin
->checkRequirements();
foreach ($source_plugin as $row) {
if ($row
->getSourceProperty('entity_type') == 'commerce_product') {
$fields[$row
->getSourceProperty('bundle')][$row
->getSourceProperty('field_name')] = $row
->getSource();
}
}
} catch (RequirementsException $e) {
}
try {
foreach ($product_variation_types as $row) {
$product_variation_type = $row
->getSourceProperty('type');
$values = $base_plugin_definition;
$values['label'] = $this
->t('@label (@type)', [
'@label' => $values['label'],
'@type' => $row
->getSourceProperty('name'),
]);
$values['source']['product_variation_type'] = $product_variation_type;
$values['destination']['default_bundle'] = $product_variation_type;
$migration = \Drupal::service('plugin.manager.migration')
->createStubMigration($values);
$this->fieldDiscovery
->addBundleFieldProcesses($migration, 'commerce_product', $product_variation_type);
$this->derivatives[$product_variation_type] = $migration
->getPluginDefinition();
}
} catch (DatabaseExceptionWrapper $e) {
}
return parent::getDerivativeDefinitions($base_plugin_definition);
}
}