ProductType.php 1.66 KB
Newer Older
1 2
<?php

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

use Drupal\migrate_drupal\Plugin\migrate\source\DrupalSqlBase;
6
use Drupal\migrate\Row;
7 8

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

  /**
19
   * {@inheritdoc}
20 21 22 23 24 25 26 27
   */
  public function fields() {
    return [
      'type' => t('Type'),
      'name' => t('Name'),
      'description' => t('Description'),
      'help' => t('Help'),
      'revision' => t('Revision'),
28
      'line_item_type' => t('Line item type'),
29 30 31 32
    ];
  }

  /**
33
   * {@inheritdoc}
34 35
   */
  public function getIds() {
36
    $ids['type']['type'] = 'string';
37 38 39 40
    $ids['type']['alias'] = 'pt';
    return $ids;
  }

41 42 43 44
  /**
   * {@inheritdoc}
   */
  public function prepareRow(Row $row) {
45 46 47 48 49 50 51 52 53
    // Add the line item type for this product type, if it exists.
    $type = $row->getSourceProperty('type');
    $data = $this->select('field_config_instance', 'fci')
      ->fields('fci', ['data'])
      ->condition('fci.bundle', $type)
      ->condition('data', '%line_item_type%', 'LIKE')
      ->execute()
      ->fetchCol();
    $data = empty($data) ?: unserialize($data[0]);
54 55 56 57 58
    $line_item_type = isset($data['display']['default']['settings']['line_item_type']) ? $data['display']['default']['settings']['line_item_type'] : '';
    $row->setSourceProperty('line_item_type', $line_item_type);
    return parent::prepareRow($row);
  }

59
  /**
60
   * {@inheritdoc}
61 62
   */
  public function query() {
63
    $query = $this->select('commerce_product_type', 'pt')->fields('pt');
64
    return $query;
65
  }
bojanz's avatar
bojanz committed
66

67
}