commerce_order.install 10.7 KB
Newer Older
1
2
3
4
5
6
7
<?php

/**
 * @file
 * Install, update and uninstall functions for the Order module.
 */

8
use Drupal\commerce\CommerceContentEntityStorageSchema;
9
use Drupal\Core\Field\BaseFieldDefinition;
10
11
12
13
14
15
16
17
18
use Drupal\Core\Session\AccountInterface;

/**
 * Implements hook_install().
 */
function commerce_order_install() {
  // Allow authenticated users to view own orders.
  user_role_grant_permissions(AccountInterface::AUTHENTICATED_ROLE, ['view own commerce_order']);
}
19

20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
/**
 * Implements hook_update_dependencies().
 */
function commerce_order_update_dependencies() {
  if (!\Drupal::moduleHandler()->moduleExists('commerce_product')) {
    return [];
  }

  $dependencies = [];
  // Make sure commerce_order_update_8207 is executed after
  // commerce_product_update_8209, so that the 'stores' and 'variations'
  // configurable fields are converted to base fields before the
  // commerce_number_pattern module is enabled.
  $dependencies['commerce_order'][8207] = [
    'commerce_product' => 8209,
  ];

  return $dependencies;
}

40
41
42
43
44
45
46
47
48
49
50
/**
 * Add the 'data' field to 'commerce_order_item' entities.
 */
function commerce_order_update_8201() {
  $storage_definition = BaseFieldDefinition::create('map')
    ->setLabel(t('Data'))
    ->setDescription(t('A serialized array of additional data.'));

  $update_manager = \Drupal::entityDefinitionUpdateManager();
  $update_manager->installFieldStorageDefinition('data', 'commerce_order_item', 'commerce_order', $storage_definition);
}
51
52
53
54
55
56
57
58
59
60
61
62
63

/**
 * Add the 'overridden_unit_price' field to 'commerce_order_item' entities.
 */
function commerce_order_update_8202() {
  $storage_definition = BaseFieldDefinition::create('boolean')
    ->setLabel(t('Overridden unit price'))
    ->setDescription(t('Whether the unit price is overridden.'))
    ->setDefaultValue(FALSE);

  $update_manager = \Drupal::entityDefinitionUpdateManager();
  $update_manager->installFieldStorageDefinition('overridden_unit_price', 'commerce_order_item', 'commerce_order', $storage_definition);
}
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79

/**
 * Add the 'locked' field to 'commerce_order' entities.
 */
function commerce_order_update_8203() {
  $storage_definition = BaseFieldDefinition::create('boolean')
    ->setLabel(t('Locked'))
    ->setSettings([
      'on_label' => t('Yes'),
      'off_label' => t('No'),
    ])
    ->setDefaultValue(FALSE);

  $update_manager = \Drupal::entityDefinitionUpdateManager();
  $update_manager->installFieldStorageDefinition('locked', 'commerce_order', 'commerce_order', $storage_definition);
}
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96

/**
 * Add the 'uses_legacy_adjustments' field to 'commerce_order_item' entities.
 */
function commerce_order_update_8204() {
  $storage_definition = BaseFieldDefinition::create('boolean')
    ->setLabel(t('Uses legacy adjustments'))
    ->setSettings([
      'on_label' => t('Yes'),
      'off_label' => t('No'),
    ])
    ->setDefaultValue(FALSE)
    ->setInitialValue(TRUE);

  $update_manager = \Drupal::entityDefinitionUpdateManager();
  $update_manager->installFieldStorageDefinition('uses_legacy_adjustments', 'commerce_order_item', 'commerce_order', $storage_definition);
}
97
98
99
100
101
102
103
104
105
106
107
108
109
110

/**
 * Add the 'total_paid' field to 'commerce_order' entities.
 */
function commerce_order_update_8205() {
  $storage_definition = BaseFieldDefinition::create('commerce_price')
    ->setLabel(t('Total paid'))
    ->setDescription(t('The total paid price of the order.'))
    ->setDisplayConfigurable('form', FALSE)
    ->setDisplayConfigurable('view', TRUE);

  $update_manager = \Drupal::entityDefinitionUpdateManager();
  $update_manager->installFieldStorageDefinition('total_paid', 'commerce_order', 'commerce_order', $storage_definition);
}
111
112
113
114
115
116
117
118
119
120

/**
 * Clears the address book for all customers, to avoid duplicates.
 */
function commerce_order_update_8206() {
  // Bypass Entity API for better performance.
  $database = \Drupal::database();
  $database->query("UPDATE {profile} SET uid = 0 WHERE type = 'customer'");
  $database->query("UPDATE {profile_revision} SET uid = 0 WHERE profile_id IN (SELECT profile_id FROM {profile} WHERE type = 'customer')");
}
121
122
123
124
125
126
127

/**
 * Install the Commerce Number Pattern module.
 */
function commerce_order_update_8207() {
  \Drupal::service('module_installer')->install(['commerce_number_pattern']);
}
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
166
167
168
169
170
171
172
173
174
175
176
177
178

/**
 * Convert the 'order_items' configurable field into a base field.
 */
function commerce_order_update_8208() {
  $bundle_info = \Drupal::service('entity_type.bundle.info')->getBundleInfo('commerce_order');
  $order_types = array_keys($bundle_info);
  $config_factory = \Drupal::configFactory();
  $config_manager = \Drupal::service('config.manager');
  $entity_type_manager = \Drupal::entityTypeManager();
  $entity_field_manager = \Drupal::service('entity_field.manager');

  $entity_type_manager->clearCachedDefinitions();
  $entity_field_manager->clearCachedFieldDefinitions();

  $key = 'commerce_order.field_storage_definitions';
  $entity_definitions_installed = \Drupal::keyValue('entity.definitions.installed');
  $definitions = $entity_definitions_installed->get($key);
  /** @var \Drupal\Core\Field\FieldDefinitionInterface[] $base_field_definitions */
  $base_field_definitions = $entity_field_manager->getBaseFieldDefinitions('commerce_order');
  $definitions['order_items'] = $base_field_definitions['order_items']->getFieldStorageDefinition();
  $entity_definitions_installed->set($key, $definitions);

  $configuration_to_delete = [];
  foreach ($order_types as $bundle) {
    $configuration_to_delete[] = 'field.field.commerce_order.' . $bundle . '.order_items';
  }
  $configuration_to_delete[] = 'field.storage.commerce_order.order_items';

  $dependents = $config_manager->findConfigEntityDependents('config', $configuration_to_delete);
  /** @var \Drupal\Core\Config\Entity\ConfigEntityInterface $config_entity */
  foreach ($dependents as $config_name => $config_entity) {
    $config_entity = $config_factory->getEditable($config_name);
    $dependencies = $config_entity->get('dependencies.config');
    $dependencies = array_diff($dependencies, $configuration_to_delete);
    $config_entity->set('dependencies.config', $dependencies);
    $config_entity->save();
  }

  foreach ($configuration_to_delete as $config) {
    $config_factory->getEditable($config)->delete();
  }
}

/**
 * Update all existing views using a reference to order items.
 */
function commerce_order_update_8209() {
  $config_factory = \Drupal::configFactory();
  foreach ($config_factory->listAll('views.view.') as $view_config_name) {
    $view = $config_factory->getEditable($view_config_name);
179
180
181
182
183
    if ($view->get('base_field') != 'order_id') {
      // Not a commerce_order SQL view.
      continue;
    }

184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
    $displays = $view->get('display');
    foreach ($displays as $display_name => $display) {
      if (!empty($display['display_options']['relationships'])) {
        foreach ($display['display_options']['relationships'] as $relationship_name => $relationship) {
          if ($relationship_name == 'order_items') {
            // Update the relation field and add entity information.
            $base = "display.$display_name.display_options.relationships.$relationship_name";
            $view->set($base . '.field', $relationship_name . '_target_id');
            $view->set($base . '.entity_type', 'commerce_order');
            $view->set($base . '.entity_field', $relationship_name);
          }
        }
      }

      if (!empty($display['display_options']['fields'])) {
        foreach ($display['display_options']['fields'] as $field_name => $field) {
          if ($field_name == 'order_items') {
            // Update the relation field and add entity information.
            $base = "display.$display_name.display_options.fields.$field_name";
            $view->set($base . '.field', $field_name . '_target_id');
          }
        }
      }
    }
    $view->save(TRUE);
  }
}
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226

/**
 * Ensure new field indexes on the order entity.
 */
function commerce_order_update_8210() {
  $entity_type_manager = \Drupal::entityTypeManager();
  $definition_update_manager = \Drupal::entityDefinitionUpdateManager();

  // Get the current order entity type definition, ensure the storage schema
  // class is set.
  $entity_type = $entity_type_manager->getDefinition('commerce_order')
    ->setHandlerClass('storage_schema', CommerceContentEntityStorageSchema::class);

  // Regenerate entity type indexes.
  $definition_update_manager->updateEntityType($entity_type);
}
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243

/**
 * Add the 'locked' field to 'commerce_order_item' entities.
 */
function commerce_order_update_8211() {
  $storage_definition = BaseFieldDefinition::create('boolean')
    ->setLabel(t('Locked'))
    ->setSettings([
      'on_label' => t('Yes'),
      'off_label' => t('No'),
    ])
    ->setDefaultValue(FALSE)
    ->setInitialValue(FALSE);

  $update_manager = \Drupal::entityDefinitionUpdateManager();
  $update_manager->installFieldStorageDefinition('locked', 'commerce_order_item', 'commerce_order_item', $storage_definition);
}
244
245
246
247
248
249
250
251

/**
 * Update the order_number field definition.
 */
function commerce_order_update_8212() {
  $definition_update_manager = \Drupal::service('entity.definition_update_manager');
  $definition_update_manager->updateFieldStorageDefinition($definition_update_manager->getFieldStorageDefinition('order_number', 'commerce_order'));
}
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271

/**
 * Add the 'balance' field to the 'default' view mode for all order bundles.
 */
function commerce_order_update_8213() {
  $bundles = array_keys(\Drupal::service('entity_type.bundle.info')->getBundleInfo('commerce_order'));
  $storage = \Drupal::entityTypeManager()->getStorage('entity_view_display');
  foreach ($bundles as $bundle) {
    /** @var \Drupal\Core\Entity\Display\EntityViewDisplayInterface $default_display */
    $default_display = $storage->load('commerce_order.' . $bundle . '.default');
    if (!$default_display) {
      continue;
    }
    $default_display->setComponent('balance', [
      'type' => 'commerce_price_default',
      'label' => 'inline',
    ]);
    $default_display->save();
  }
}
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290

/**
 * Add the 'version' field to 'commerce_order' entities.
 */
function commerce_order_update_8214() {
  $storage_definition = BaseFieldDefinition::create('integer')
    ->setLabel(t('Version'))
    ->setDescription(t('The order version number, it gets incremented on each save.'))
    ->setReadOnly(TRUE)
    ->setSetting('unsigned', TRUE)
    ->setDefaultValue(0)
    // Ensure any existing orders get set to version 1.
    ->setInitialValue(1);

  $update_manager = \Drupal::entityDefinitionUpdateManager();
  $update_manager->installFieldStorageDefinition('version', 'commerce_order', 'commerce_order', $storage_definition);

  return t('The order version number field was created.');
}
291
292
293
294
295
296
297
298

/**
 * Update the state field definition.
 */
function commerce_order_update_8215() {
  $definition_update_manager = \Drupal::entityDefinitionUpdateManager();
  $definition_update_manager->updateFieldStorageDefinition($definition_update_manager->getFieldStorageDefinition('state', 'commerce_order'));
}