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

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

use Drupal\Core\Field\BaseFieldDefinition;
9 10 11 12 13 14 15 16 17
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']);
}
18

19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
/**
 * 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;
}

39 40 41 42 43 44 45 46 47 48 49
/**
 * 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);
}
50 51 52 53 54 55 56 57 58 59 60 61 62

/**
 * 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);
}
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78

/**
 * 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);
}
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95

/**
 * 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);
}
96 97 98 99 100 101 102 103 104 105 106 107 108 109

/**
 * 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);
}
110 111 112 113 114 115 116 117 118 119

/**
 * 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')");
}
120 121 122 123 124 125 126

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

/**
 * 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);
178 179 180 181 182
    if ($view->get('base_field') != 'order_id') {
      // Not a commerce_order SQL view.
      continue;
    }

183 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
    $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);
  }
}