1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @file |
5
|
|
|
* Migrations for Commerce Nodes with Product References. |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
class DemoFrameworkBaseCommerceNodes extends LightningModerationNodes { |
9
|
|
|
|
10
|
|
|
public function __construct($arguments) { |
11
|
|
|
parent::__construct($arguments); |
12
|
|
|
$this->description = t('Import commerce product nodes.'); |
13
|
|
|
|
14
|
|
|
// Create a map object for tracking the relationships between source rows |
15
|
|
|
$this->map = new MigrateSQLMap($this->machineName, |
16
|
|
|
array( |
17
|
|
|
'title' => array( |
18
|
|
|
'type' => 'varchar', |
19
|
|
|
'length' => 255, |
20
|
|
|
'not null' => TRUE, |
21
|
|
|
), |
22
|
|
|
), |
23
|
|
|
MigrateDestinationNode::getKeySchema() |
24
|
|
|
); |
25
|
|
|
|
26
|
|
|
$import_path = drupal_get_path('module', 'df_commerce') . '/import/'; |
27
|
|
|
$this->source = new MigrateSourceCSV($import_path . 'df_commerce.base.nodes.commerce.csv', $this->csvcolumns(), array('header_rows' => 1)); |
28
|
|
|
|
29
|
|
|
$this->destination = new MigrateDestinationNode('commerce'); |
30
|
|
|
|
31
|
|
|
$this->addFieldMapping('uid', 'uid')->defaultValue(1); |
32
|
|
|
|
33
|
|
|
$this->addFieldMapping('title', 'title'); |
34
|
|
|
$this->addFieldMapping('body', 'description'); |
35
|
|
|
$this->addFieldMapping('field_product', 'skus'); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
View Code Duplication |
function csvcolumns() { |
|
|
|
|
39
|
|
|
$columns[0] = array('title', 'Title'); |
|
|
|
|
40
|
|
|
$columns[1] = array('description', 'Description'); |
41
|
|
|
$columns[2] = array('sku', 'SKU'); |
42
|
|
|
return $columns; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
View Code Duplication |
function prepareRow($row) { |
|
|
|
|
46
|
|
|
$products = array(); |
47
|
|
|
foreach (explode(', ', $row->sku) as $sku) { |
48
|
|
|
$product = commerce_product_load_by_sku($sku); |
49
|
|
|
$products[] = $product->product_id; |
50
|
|
|
} |
51
|
|
|
$row->skus = $products; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.