-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcommerce_xquantity.install
More file actions
136 lines (118 loc) · 4.99 KB
/
commerce_xquantity.install
File metadata and controls
136 lines (118 loc) · 4.99 KB
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
<?php
/**
* @file
* Install, update and uninstall functions for the xquantity module.
*/
use Drupal\commerce_xquantity\Entity\XquantityOrderItem;
use Drupal\commerce_order\Entity\OrderItem;
/**
* Implements hook_install().
*
* Update order item xquantity field storage definition.
*
* @see https://www.drupal.org/node/2794909
*/
function commerce_xquantity_install() {
$config = \Drupal::configFactory();
$key_value = \Drupal::keyValue('entity.definitions.installed');
$database = \Drupal::database();
$db_schema = $database->schema();
$all = $updated = [];
foreach ($config->listAll('core.entity_form_display.commerce_order_item.') as $id) {
$editable = $config->getEditable($id);
$data = $editable->getRawData();
if (isset($data['targetEntityType'])) {
$entity_type = $data['targetEntityType'];
$field_name = 'quantity';
$definitions = $key_value->get("{$entity_type}.field_storage_definitions");
if (!isset($definitions[$field_name])) {
continue;
}
$changes = $definitions[$field_name]->getSchema()['columns']['value'];
$needs_change = $changes['precision'] != 14 && $changes['scale'] != 4;
$changes['precision'] = 14;
$changes['scale'] = 4;
$tables = ["{$entity_type}_revision", $entity_type];
foreach ($tables as $table) {
if ($needs_change && $db_schema->tableExists($table) && !isset($updated[$table])) {
$updated[$table] = FALSE;
// The table data to restore after the update is completed.
$all[$table] = $database->select($table, 'n')
->fields('n')
->execute()
->fetchAll();
// Truncate the field table to unlock it for changes.
$database
->truncate($table)
->execute();
$db_schema->changeField($table, $field_name, $field_name, $changes);
$updated[$table] = TRUE;
}
}
if (isset($data['content'][$field_name]) && $data['content'][$field_name]['type'] !== 'xnumber') {
$data['content'][$field_name]['type'] = 'xnumber';
$data['content'][$field_name]['settings'] += [
'min' => '1',
'max' => "",
'default_value' => '1',
'step' => '1',
'prefix' => '',
'suffix' => '',
'disable_on_cart' => '0',
];
$editable->setData($data);
$editable->save();
}
}
}
// Restore earlier saved number fields data.
foreach ($all as $table => $rows) {
$updated[$table] = FALSE;
foreach ($rows as $row) {
$database->insert($table)
->fields((array) $row)
->execute();
}
$updated[$table] = TRUE;
}
if (!empty($updated) && !in_array(FALSE, $updated)) {
$update_manager = \Drupal::entityDefinitionUpdateManager();
$entity_type = $update_manager->getEntityType('commerce_order_item');
$update_manager->installFieldStorageDefinition('quantity', 'commerce_order_item', 'commerce_order', XquantityOrderItem::baseFieldDefinitions($entity_type)['quantity']);
\Drupal::messenger()->addMessage(t('The order item quantity field definition has been successfully updated.'));
}
else {
\Drupal::messenger()->addMessage(t("The attempt to update order item quantity field is failed. To update the field manually go to commerce_order_item table in the site's DB and edit quantity field structure changing its Length to 14,4. Then flush caches and set Xnumber field widget for each of the order item type's enabled form display modes."), 'warning');
}
}
/**
* Implements hook_uninstall().
*
* Update order item quantity field storage definition.
*/
function commerce_xquantity_uninstall() {
$update_manager = \Drupal::entityDefinitionUpdateManager();
$entity_type = $update_manager->getEntityType('commerce_order_item');
$update_manager->installFieldStorageDefinition('quantity', 'commerce_order_item', 'commerce_order', OrderItem::baseFieldDefinitions($entity_type)['quantity']);
$config = \Drupal::configFactory();
foreach ($config->listAll('core.entity_form_display.commerce_order_item.') as $id) {
$editable = $config->getEditable($id);
$data = $editable->getRawData();
unset($data['content']['quantity']['settings']['disable_on_cart']);
$editable->setData($data);
$editable->save();
}
}
/**
* Update XquantityOrderItem 'quantity' field definition.
*/
function commerce_xquantity_update_8201() {
$update_manager = \Drupal::entityDefinitionUpdateManager();
$quantity = $update_manager->getFieldStorageDefinition('quantity', 'commerce_order_item');
if ($quantity->getType() != 'xdecimal') {
$entity_type = $update_manager->getEntityType('commerce_order_item');
$update_manager->installFieldStorageDefinition('quantity', 'commerce_order_item', 'commerce_order', XquantityOrderItem::baseFieldDefinitions($entity_type)['quantity']);
return t('Order item quantity field successfully updated to xdecimal type.');
}
return t('Order item quantity field does not need to be updated as that is of xdecimal type.');
}