MigratePrepareRow.php in Commerce Migrate 8.2
File
modules/commerce/src/EventSubscriber/MigratePrepareRow.php
View source
<?php
namespace Drupal\commerce_migrate_commerce\EventSubscriber;
use Drupal\field\Plugin\migrate\source\d7\FieldInstance;
use Drupal\migrate\Plugin\MigrationDeriverTrait;
use Drupal\migrate_plus\Event\MigrateEvents;
use Drupal\migrate_plus\Event\MigratePrepareRowEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class MigratePrepareRow implements EventSubscriberInterface {
protected $productTypes = [];
public static function getSubscribedEvents() {
$events[MigrateEvents::PREPARE_ROW][] = 'prepareRow';
return $events;
}
public function prepareRow(MigratePrepareRowEvent $event) {
$migration = $event
->getMigration();
$row = $event
->getRow();
$source_plugin = $migration
->getSourcePlugin();
if (is_a($source_plugin, FieldInstance::class)) {
$row
->setSourceProperty('commerce1_entity_type', $row
->getSourceProperty('entity_type'));
if ($row
->getSourceProperty('entity_type') === 'node') {
if (in_array($row
->getSourceProperty('bundle'), $this
->getProductTypes($event))) {
$row
->setSourceProperty('commerce1_entity_type', 'product_display');
}
}
}
}
protected function getProductTypes(MigratePrepareRowEvent $event) {
if (!empty($this->productTypes)) {
return $this->productTypes;
}
$migration = $event
->getMigration();
$source_plugin = $migration
->getSourcePlugin();
if (method_exists($source_plugin, 'getDatabase')) {
$connection = $source_plugin
->getDatabase();
if ($connection
->schema()
->tableExists('node_type')) {
$query = $connection
->select('commerce_product_type', 'pt')
->fields('pt', [
'type',
]);
$product_node_types = $query
->execute()
->fetchCol();
return $product_node_types;
}
}
return [];
}
protected function getAttributes() {
$source_plugin = MigrationDeriverTrait::getSourcePlugin('d7_field_instance');
$attributes = [];
foreach ($source_plugin as $row) {
if ($row
->getSourceProperty('entity_type') == 'commerce_product' && $row
->getSourceProperty('type') == 'taxonomy_term_reference' && $row
->getSourceProperty('widget')['type'] == 'options_select') {
$name = str_replace('field_', '', $row
->getSourceProperty('field_name'));
$attributes[] = $name;
}
}
return array_unique($attributes);
}
}