View source
<?php
namespace Drupal\commerce_migrate_commerce\Plugin\migrate\source\commerce1;
use Drupal\migrate\MigrateException;
use Drupal\migrate\Row;
use Drupal\migrate_drupal\Plugin\migrate\source\DrupalSqlBase;
use Drupal\commerce_product\Entity\ProductType as CommerceProductType;
class ProductDisplayType extends DrupalSqlBase {
public function fields() {
return [
'field_name' => t('Product reference field name'),
'type' => t('Type'),
'name' => t('Name'),
'description' => t('Description'),
'help' => t('Help'),
'data' => t('Product reference field instance data'),
'variation_type' => t('Product variation type'),
];
}
public function getIds() {
$ids['type']['type'] = 'string';
$ids['type']['alias'] = 'nt';
return $ids;
}
public function prepareRow(Row $row) {
$row
->setSourceProperty('data', unserialize($row
->getSourceProperty('data')));
$instance_config = $row
->getSourceProperty('data');
$product_variation_type = array_filter($instance_config['settings']['referenceable_types']);
if (count($product_variation_type) > 1) {
$product_variation_type = $this
->resolveTargetVariationType($row, $product_variation_type);
}
else {
$product_variation_type = reset($product_variation_type);
}
$row
->setSourceProperty('variation_type', $product_variation_type);
return parent::prepareRow($row);
}
public function query() {
$query = $this
->select('field_config', 'fc');
$query
->leftJoin('field_config_instance', 'fci', '(fci.field_id = fc.id)');
$query
->leftJoin('node_type', 'nt', '(nt.type = fci.bundle)');
$query
->condition('fc.type', 'commerce_product_reference')
->condition('fc.active', 1)
->condition('fci.entity_type', 'node')
->condition('nt.disabled', 0);
$query
->fields('fc', [
'field_name',
])
->fields('fci', [
'data',
])
->fields('nt', [
'type',
'name',
'description',
'help',
]);
return $query;
}
public function resolveTargetVariationType(Row $row, array $product_variation_types) {
@trigger_error('ProductDisplayType::resolveTargetVariationType() is deprecated in Commerce Migrate 8.x-2.x-beta11 and will be removed before Commerce Migrate 8.x-3.x. Instead, you should use the ResolveProductVariationType process plugin. See https://www.drupal.org/node/2982007', E_USER_DEPRECATED);
$product_variation_type = FALSE;
if (isset($this->configuration['variations']['matching'])) {
$key = array_search($row
->getSourceProperty('type'), $product_variation_types);
if ($key !== FALSE) {
$product_variation_type = $product_variation_types[$key];
}
}
if ($product_variation_type === FALSE) {
if (!empty($this->configuration['variations']['default']) && CommerceProductType::load($this->configuration['variations']['default'])) {
$product_variation_type = $this->configuration['variations']['default'];
}
else {
$product_type = $row
->getSourceProperty('type');
throw new MigrateException("A product variation type could not be determined for the product type: {$product_type}");
}
}
return $product_variation_type;
}
}