-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathcommerce_canadapost.install
165 lines (143 loc) · 5.58 KB
/
commerce_canadapost.install
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
<?php
/**
* @file
* Install file for Commerce Canada Post module.
*/
use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;
use Symfony\Component\Yaml\Yaml;
/**
* Add Commerce Canada Post as a dependency to config files.
*/
function commerce_canadapost_update_8001() {
$config_factory = \Drupal::configFactory();
// Add dependency to Canada Post shipment type.
$config = $config_factory->getEditable('commerce_shipping.commerce_shipment_type.canadapost');
$config->set('dependencies.module', ['commerce_canadapost']);
$config->set('dependencies.enforced.module', ['commerce_canadapost']);
$config->save();
// Add dependency to Canada Post field storage fields.
$configs = [
'field.storage.commerce_shipment.field_actual_delivery',
'field.storage.commerce_shipment.field_attempted_delivery',
'field.storage.commerce_shipment.field_expected_delivery',
'field.storage.commerce_shipment.field_mailed_on',
];
foreach ($configs as $config) {
$config = $config_factory->getEditable($config);
$config->set('dependencies.config', ['commerce_shipping.commerce_shipment_type.canadapost']);
$config->save();
}
}
/**
* Copy Canada Post API sitewide settings to all existing store entities.
*/
function commerce_canadapost_update_8002() {
$config_factory = \Drupal::configFactory();
// Fetch the Canada Post API sitewide settings.
$config_id = 'commerce_canadapost.settings';
$config = $config_factory->get($config_id);
/** @var \Drupal\commerce_canadapost\UtilitiesService $utilities_service */
$utilities_service = \Drupal::service('commerce_canadapost.utilities_service');
$values = [];
foreach ($utilities_service->getApiKeys() as $key) {
$values[$key] = $config->get("api.$key");
}
// Grab all existing stores.
$stores = \Drupal::entityTypeManager()
->getStorage('commerce_store')
->loadMultiple();
// Save the settings to the store.
foreach ($stores as $store) {
$json_encoded_settings = $utilities_service->encodeSettings($values);
$store->set('canadapost_api_settings', $json_encoded_settings);
$store->save();
}
// Finally, delete the sitewide settings config.
$config = $config_factory->getEditable($config_id);
$config->delete();
}
/**
* Update Commerce Canada Post shipment type field names.
*/
function commerce_canadapost_update_8003() {
$new_fields = [
'field_actual_delivery' => 'canadapost_actual_delivery',
'field_attempted_delivery' => 'canadapost_attempted_delivery',
'field_current_location' => 'canadapost_current_location',
'field_expected_delivery' => 'canadapost_expected_delivery',
'field_mailed_on' => 'canadapost_mailed_on',
];
// Create the new fields in the database.
foreach ($new_fields as $old_field => $new_field) {
$module_path = drupal_get_path('module', 'commerce_canadapost');
// Create the field storage.
$yml = Yaml::parse(file_get_contents($module_path . '/config/install/field.storage.commerce_shipment.' . $new_field . '.yml'));
if (!FieldStorageConfig::loadByName($yml['entity_type'], $yml['field_name'])) {
FieldStorageConfig::create($yml)->save();
}
// Create the field.
$yml = Yaml::parse(file_get_contents($module_path . '/config/install/field.field.commerce_shipment.canadapost.' . $new_field . '.yml'));
if (!FieldConfig::loadByName($yml['entity_type'], $yml['bundle'], $yml['field_name'])) {
FieldConfig::create($yml)->save();
}
}
// Re-import our default configs.
\Drupal::service('config.installer')->installDefaultConfig('module', 'commerce_canadapost');
// Copy over the data from the old fields to the new fields.
_commerce_canadapost_copy_field_data($new_fields);
// Finally, let's delete the old fields.
foreach ($new_fields as $old_field => $new_field) {
// Delete the field storage.
$field = FieldStorageConfig::loadByName('commerce_shipment', $old_field);
if ($field) {
$field->delete();
}
// Delete the field.
$field = FieldConfig::loadByName('commerce_shipment', 'canadapost', $old_field);
if ($field) {
$field->delete();
}
}
}
/**
* Copy over the old field data to the new renamed fields.
*
* @param array $new_fields
* The old and new field names.
*/
function _commerce_canadapost_copy_field_data(array $new_fields) {
/** @var \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager */
$entity_type_manager = \Drupal::entityTypeManager();
/** @var \Drupal\Core\Entity\EntityStorageInterface $shipment_storage */
$shipment_storage = $entity_type_manager->getStorage('commerce_shipment');
if (!isset($sandbox['max'])) {
$sandbox['current'] = 0;
$sandbox['count'] = 0;
$sandbox['max'] = $shipment_storage->getQuery()
->accessCheck(FALSE)
->condition('type', 'canadapost')
->count()->execute();
}
$shipment_ids = $shipment_storage->getQuery()
->accessCheck(FALSE)
->range(0, 50)
->condition('type', 'canadapost')
->condition('shipment_id', $sandbox['current'], '>')
->sort('shipment_id', 'ASC')
->execute();
$shipments = $shipment_storage->loadMultiple($shipment_ids);
/** @var \Drupal\node\NodeInterface $node */
foreach ($shipments as $shipment) {
foreach ($new_fields as $old_field => $new_field) {
if ($shipment->hasField($old_field)) {
$field_value = $shipment->get($old_field)->getValue();
$shipment->set($new_field, $field_value);
}
}
$shipment->save();
$sandbox['current'] = $shipment->id();
$sandbox['count']++;
}
$sandbox['#finished'] = empty($sandbox['max']) ? 1 : $sandbox['current'] / $sandbox['max'];
}