View source
<?php
namespace Drupal\commerce_migrate_ubercart\EventSubscriber;
use Drupal\commerce_migrate\Utility;
use Drupal\field\Plugin\migrate\source\d6\Field as D6Field;
use Drupal\field\Plugin\migrate\source\d6\FieldInstance as D6FieldInstance;
use Drupal\field\Plugin\migrate\source\d6\FieldInstancePerFormDisplay as D6FieldInstancePerFormDisplay;
use Drupal\field\Plugin\migrate\source\d6\FieldInstancePerFormDisplay as D7FieldInstancePerFormDisplay;
use Drupal\field\Plugin\migrate\source\d6\FieldInstancePerViewMode as D6FieldInstancePerViewMode;
use Drupal\field\Plugin\migrate\source\d7\FieldInstance as D7FieldInstance;
use Drupal\field\Plugin\migrate\source\d7\FieldInstancePerViewMode as D7FieldInstancePerViewMode;
use Drupal\field\Plugin\migrate\source\d7\ViewMode as D7ViewMode;
use Drupal\language\Plugin\migrate\source\d6\LanguageContentSettings;
use Drupal\migrate\Plugin\MigrationInterface;
use Drupal\migrate\Row;
use Drupal\migrate_plus\Event\MigrateEvents;
use Drupal\migrate_plus\Event\MigratePrepareRowEvent;
use Drupal\node\Plugin\migrate\source\d6\NodeType as D6NodeType;
use Drupal\node\Plugin\migrate\source\d7\NodeType as D7NodeType;
use Drupal\node\Plugin\migrate\source\d6\ViewMode as D6ViewMode;
use Drupal\taxonomy\Plugin\migrate\source\d6\TermNode;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class PrepareRow implements EventSubscriberInterface {
protected $connection;
protected $productTypes = [];
public static function getSubscribedEvents() {
$events[MigrateEvents::PREPARE_ROW][] = 'onPrepareRow';
return $events;
}
public function onPrepareRow(MigratePrepareRowEvent $event) {
$migration = $event
->getMigration();
$row = $event
->getRow();
$source_plugin = $migration
->getSourcePlugin();
if (Utility::classInArray($source_plugin, [
D6NodeType::class,
TermNode::class,
D7Nodetype::class,
])) {
$node_type = $row
->getSourceProperty('type');
$this->productTypes = $this
->getProductTypes($migration);
$row
->setSourceProperty('product_type', TRUE);
if (in_array($node_type, $this->productTypes)) {
$row
->setSourceProperty('product_type', NULL);
}
}
if (is_a($source_plugin, D6Field::class)) {
$this->productTypes = $this
->getProductTypes($migration);
$field_name = $row
->getSourceProperty('field_name');
$query = $this->connection
->select('content_node_field', 'cnf')
->fields('cnfi', [
'type_name',
])
->distinct();
$query
->innerJoin('content_node_field_instance', 'cnfi', 'cnfi.field_name = cnf.field_name');
$query
->condition('cnf.field_name', $field_name);
$instances = $query
->execute()
->fetchCol();
$i = 0;
foreach ($instances as $instance) {
if (in_array($instance, $this->productTypes)) {
$i++;
}
}
if ($i > 0) {
if ($i == count($instances)) {
$row
->setSourceProperty('entity_type', 'commerce_product');
}
else {
$row
->setSourceProperty('ubercart_entity_type', 'commerce_product');
$row
->setSourceProperty('entity_type', 'node');
}
}
else {
$row
->setSourceProperty('entity_type', 'node');
}
}
if (Utility::classInArray($source_plugin, [
D6FieldInstance::class,
D6FieldInstancePerViewMode::class,
D6FieldInstancePerFormDisplay::class,
D6ViewMode::class,
], FALSE)) {
if (!$this
->setEntityType($row, $migration, $row
->getSourceProperty('type_name'))) {
$row
->setSourceProperty('entity_type', 'node');
}
}
if (Utility::classInArray($source_plugin, [
D7FieldInstance::class,
D7FieldInstancePerViewMode::class,
D7FieldInstancePerFormDisplay::class,
D7ViewMode::class,
], FALSE)) {
$this
->setCommerceProductProperty($row, $migration, $row
->getSourceProperty('bundle'));
}
if (is_a($source_plugin, LanguageContentSettings::class)) {
$node_type = $row
->getSourceProperty('type');
$this->productTypes = $this
->getProductTypes($migration);
$row
->setSourceProperty('product_type', TRUE);
if (in_array($node_type, $this->productTypes)) {
$source = $row
->getSource();
$type = $source['constants']['target_type'];
if ($type == 'node') {
$row
->setSourceProperty('product_type', NULL);
}
}
}
}
protected function setEntityType(Row $row, MigrationInterface $migration, $type_name) {
if ($this->productTypes == []) {
$this->productTypes = $this
->getProductTypes($migration);
}
if (in_array($type_name, $this->productTypes)) {
$row
->setSourceProperty('entity_type', 'commerce_product');
return TRUE;
}
return FALSE;
}
protected function setCommerceProductProperty(Row $row, MigrationInterface $migration, $type_name) {
if ($this->productTypes == []) {
$this->productTypes = $this
->getProductTypes($migration);
}
if (in_array($type_name, $this->productTypes)) {
$row
->setSourceProperty('commerce_product', 1);
return TRUE;
}
return FALSE;
}
protected function getProductTypes(MigrationInterface $migration) {
if (!empty($this->productTypes)) {
return $this->productTypes;
}
$source_plugin = $migration
->getSourcePlugin();
if (method_exists($source_plugin, 'getDatabase')) {
$this->connection = $source_plugin
->getDatabase();
if ($this->connection
->schema()
->tableExists('node_type')) {
$query = $this->connection
->select('node_type', 'nt')
->fields('nt', [
'type',
])
->condition('module', 'uc_product%', 'LIKE')
->distinct();
$this->productTypes = [
$query
->execute()
->fetchCol(),
];
}
}
return reset($this->productTypes);
}
}