ProductDisplayType.php 4.6 KB
Newer Older
1 2
<?php

3
namespace Drupal\commerce_migrate_commerce\Plugin\migrate\source\commerce1;
4 5 6 7

use Drupal\migrate\MigrateException;
use Drupal\migrate\Row;
use Drupal\migrate_drupal\Plugin\migrate\source\DrupalSqlBase;
8
use Drupal\commerce_product\Entity\ProductType as CommerceProductType;
9 10

/**
11
 * Gets Commerce 1 commerce_product_type data from database.
12 13
 *
 * @MigrateSource(
14
 *   id = "commerce1_product_display_type",
15
 *   source_module = "commerce_product"
16 17 18 19 20
 * )
 */
class ProductDisplayType extends DrupalSqlBase {

  /**
21
   * {@inheritdoc}
22 23 24 25 26 27 28 29
   */
  public function fields() {
    return [
      'field_name' => t('Product reference field name'),
      'type' => t('Type'),
      'name' => t('Name'),
      'description' => t('Description'),
      'help' => t('Help'),
bojanz's avatar
bojanz committed
30
      'data' => t('Product reference field instance data'),
31
      'variation_type' => t('Product variation type'),
32 33 34 35
    ];
  }

  /**
36
   * {@inheritdoc}
37 38 39 40 41 42 43 44 45 46 47
   */
  public function getIds() {
    $ids['type']['type'] = 'string';
    $ids['type']['alias'] = 'nt';
    return $ids;
  }

  /**
   * {@inheritdoc}
   */
  public function prepareRow(Row $row) {
48 49 50 51 52
    $row->setSourceProperty('data', unserialize($row->getSourceProperty('data')));

    // @TODO: Remove this block of code when resolveTargetVariationType is
    // removed.
    $instance_config = $row->getSourceProperty('data');
53 54 55
    $product_variation_type = array_filter($instance_config['settings']['referenceable_types']);

    if (count($product_variation_type) > 1) {
56 57 58 59
      $product_variation_type = $this->resolveTargetVariationType($row, $product_variation_type);
    }
    else {
      $product_variation_type = reset($product_variation_type);
60 61 62 63 64 65 66 67
    }

    $row->setSourceProperty('variation_type', $product_variation_type);

    return parent::prepareRow($row);
  }

  /**
68
   * {@inheritdoc}
69 70 71 72 73 74 75 76 77 78 79 80 81 82
   */
  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;
  }
bojanz's avatar
bojanz committed
83

84 85 86 87 88 89 90 91 92
  /**
   * Tries to determine a single target variation type.
   *
   * In 2.x, products can only be mapped to a single product variation type,
   * whereas in 1.x one product display node can be mapped to multiple product
   * types via the product reference field's settings.
   *
   * This function can be overwritten by custom migration classes if you need
   * different logic for determining the target variation type.
93 94 95 96 97 98 99 100 101 102
   *
   * @param \Drupal\migrate\Row $row
   *   The current row.
   * @param array $product_variation_types
   *   An array of product variation types.
   *
   * @return bool|string
   *   The product variation type matching the product, of FALSE if not found.
   *
   * @throws \Drupal\migrate\MigrateException
103 104 105 106 107
   *
   * @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
108
   */
109
  public function resolveTargetVariationType(Row $row, array $product_variation_types) {
110
    @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);
111 112
    $product_variation_type = FALSE;

113
    if (isset($this->configuration['variations']['matching'])) {
114 115 116 117 118 119 120 121 122 123
      // Try to find a variation type that matches the product type.
      $key = array_search($row->getSourceProperty('type'), $product_variation_types);

      if ($key !== FALSE) {
        $product_variation_type = $product_variation_types[$key];
      }
    }

    if ($product_variation_type === FALSE) {
      // Make sure the default product type exists.
124
      if (!empty($this->configuration['variations']['default']) && CommerceProductType::load($this->configuration['variations']['default'])) {
125 126 127 128 129 130 131 132 133 134
        $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;
  }
bojanz's avatar
bojanz committed
135

136
}