You are here

commerce_pricelist.install in Commerce Pricelist 8.2

Same filename and directory in other branches
  1. 7 commerce_pricelist.install

Install, update and uninstall functions for the Pricelist module.

File

commerce_pricelist.install
View source
<?php

/**
 * @file
 * Install, update and uninstall functions for the Pricelist module.
 */
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\commerce\CommerceContentEntityStorage;
use Drupal\commerce_pricelist\Entity\PriceList;
use Drupal\commerce_pricelist\Entity\PriceListItem;
use Drupal\commerce_pricelist\Event\PriceListEvent;
use Drupal\commerce_pricelist\Event\PriceListItemEvent;

/**
 * Replace the 'customer_role' field with the 'customer_roles' field.
 */
function commerce_pricelist_update_8201() {
  $definition_update_manager = \Drupal::entityDefinitionUpdateManager();

  // Create the customer_roles field.
  $storage_definition = BaseFieldDefinition::create('entity_reference')
    ->setLabel(t('Customer roles'))
    ->setDescription(t('The customer roles for which the price list is valid.'))
    ->setCardinality(BaseFieldDefinition::CARDINALITY_UNLIMITED)
    ->setSetting('target_type', 'user_role')
    ->setDisplayOptions('form', [
    'type' => 'options_buttons',
  ]);
  $definition_update_manager
    ->installFieldStorageDefinition('customer_roles', 'commerce_pricelist', 'commerce_pricelist', $storage_definition);

  // Seed the new field manually, because Drupal core doesn't support
  // using setInitialValueFromField() on a multivalue field.
  $database = \Drupal::database();
  $role_ids = $database
    ->select('commerce_pricelist', 'cp')
    ->fields('cp', [
    'id',
    'type',
    'customer_role',
  ])
    ->where('cp.customer_role IS NOT NULL')
    ->execute()
    ->fetchAllAssoc('id');
  $insert_query = $database
    ->insert('commerce_pricelist__customer_roles')
    ->fields([
    'bundle',
    'deleted',
    'entity_id',
    'revision_id',
    'langcode',
    'delta',
    'customer_roles_target_id',
  ]);
  foreach ($role_ids as $id => $data) {
    $insert_query
      ->values([
      $data->type,
      0,
      $id,
      $id,
      'und',
      0,
      $data->customer_role,
    ]);
  }
  $insert_query
    ->execute();

  // Remove the customer_role field.
  $storage_definition = BaseFieldDefinition::create('entity_reference')
    ->setName('customer_role')
    ->setTargetEntityTypeId('commerce_pricelist')
    ->setLabel(t('Customer role'))
    ->setDescription(t('The customer role for which the price list is valid.'))
    ->setSetting('target_type', 'user_role')
    ->setDisplayOptions('form', [
    'type' => 'options_select',
  ]);
  $definition_update_manager
    ->uninstallFieldStorageDefinition($storage_definition);
}

/**
 * Define event handlers for price list and price list items.
 */
function commerce_pricelist_update_8202() {
  $definition_update_manager = \Drupal::entityDefinitionUpdateManager();
  $entity_type = $definition_update_manager
    ->getEntityType('commerce_pricelist');
  $entity_type
    ->setHandlerClass('event', PriceListEvent::class);
  $entity_type
    ->setHandlerClass('storage', CommerceContentEntityStorage::class);
  $definition_update_manager
    ->updateEntityType($entity_type);
  $entity_type = $definition_update_manager
    ->getEntityType('commerce_pricelist_item');
  $entity_type
    ->setHandlerClass('event', PriceListItemEvent::class);
  $entity_type
    ->setHandlerClass('storage', CommerceContentEntityStorage::class);
  $definition_update_manager
    ->updateEntityType($entity_type);
}

/**
 * Add a 'uid' field to price lists and price list items.
 */
function commerce_pricelist_update_8203() {

  /** @var \Drupal\commerce_store\StoreStorageInterface $store_storage */
  $store_storage = \Drupal::entityTypeManager()
    ->getStorage('commerce_store');
  $default_store = $store_storage
    ->loadDefault();

  // Defaults the price list owner to the store owner.
  $default_uid = $default_store ? $default_store
    ->getOwnerId() : 0;
  $definition_update_manager = \Drupal::entityDefinitionUpdateManager();
  $storage_definition = BaseFieldDefinition::create('entity_reference')
    ->setLabel(t('Owner'))
    ->setDescription(t('The user that owns this price list.'))
    ->setSetting('target_type', 'user')
    ->setTranslatable(FALSE)
    ->setInitialValue($default_uid)
    ->setDefaultValueCallback(PriceList::class . '::getDefaultEntityOwner');
  $definition_update_manager
    ->installFieldStorageDefinition('uid', 'commerce_pricelist', 'commerce_pricelist', $storage_definition);
  $storage_definition = BaseFieldDefinition::create('entity_reference')
    ->setLabel(t('Owner'))
    ->setDescription(t('The user that owns this price list item.'))
    ->setSetting('target_type', 'user')
    ->setTranslatable(FALSE)
    ->setInitialValue($default_uid)
    ->setDefaultValueCallback(PriceListItem::class . '::getDefaultEntityOwner');
  $definition_update_manager
    ->installFieldStorageDefinition('uid', 'commerce_pricelist_item', 'commerce_pricelist', $storage_definition);
  foreach ([
    'commerce_pricelist',
    'commerce_pricelist_item',
  ] as $entity_type_id) {
    $entity_type = $definition_update_manager
      ->getEntityType($entity_type_id);
    $keys = $entity_type
      ->getKeys();
    $keys['owner'] = 'uid';
    $keys['uid'] = 'uid';
    $entity_type
      ->set('entity_keys', $keys);
    $definition_update_manager
      ->updateEntityType($entity_type);
  }
}

/**
 * Fix the entity definition mismatch caused by the "data_table" key removal.
 */
function commerce_pricelist_update_8204() {

  /** @var \Drupal\Core\Entity\EntityLastInstalledSchemaRepositoryInterface $last_installed_schema_repository */
  $last_installed_schema_repository = \Drupal::service('entity.last_installed_schema.repository');
  foreach ([
    'commerce_pricelist',
    'commerce_pricelist_item',
  ] as $entity_type_id) {
    $entity_type = $last_installed_schema_repository
      ->getLastInstalledDefinition($entity_type_id);
    $entity_type
      ->set('data_table', NULL);
    $last_installed_schema_repository
      ->setLastInstalledDefinition($entity_type);
  }
}

/**
 * Replace the 'customer' field with the 'customers' field.
 */
function commerce_pricelist_update_8205() {
  $definition_update_manager = \Drupal::entityDefinitionUpdateManager();

  // Create the customers field.
  $storage_definition = BaseFieldDefinition::create('entity_reference')
    ->setLabel(t('Customers'))
    ->setDescription(t('The customers for which the price list is valid.'))
    ->setCardinality(BaseFieldDefinition::CARDINALITY_UNLIMITED)
    ->setSetting('target_type', 'user')
    ->setDisplayOptions('form', [
    'type' => 'entity_reference_autocomplete',
    'settings' => [
      'match_operator' => 'CONTAINS',
      'size' => '60',
      'placeholder' => '',
    ],
  ]);
  $definition_update_manager
    ->installFieldStorageDefinition('customers', 'commerce_pricelist', 'commerce_pricelist', $storage_definition);

  // Seed the new field manually, because Drupal core doesn't support
  // using setInitialValueFromField() on a multivalue field.
  $database = \Drupal::database();
  $customer_ids = $database
    ->select('commerce_pricelist', 'cp')
    ->fields('cp', [
    'id',
    'type',
    'customer',
  ])
    ->where('cp.customer IS NOT NULL')
    ->execute()
    ->fetchAllAssoc('id');
  $insert_query = $database
    ->insert('commerce_pricelist__customers')
    ->fields([
    'bundle',
    'deleted',
    'entity_id',
    'revision_id',
    'langcode',
    'delta',
    'customers_target_id',
  ]);
  foreach ($customer_ids as $id => $data) {
    $insert_query
      ->values([
      $data->type,
      0,
      $id,
      $id,
      'und',
      0,
      $data->customer,
    ]);
  }
  $insert_query
    ->execute();

  // Remove the customer field.
  $storage_definition = BaseFieldDefinition::create('entity_reference')
    ->setLabel(t('Customer'))
    ->setName('customer')
    ->setTargetEntityTypeId('commerce_pricelist')
    ->setDescription(t('The customer for which the price list is valid.'))
    ->setSetting('target_type', 'user')
    ->setSetting('handler', 'default')
    ->setDisplayOptions('form', [
    'type' => 'entity_reference_autocomplete',
    'settings' => [
      'match_operator' => 'CONTAINS',
      'size' => '60',
      'autocomplete_type' => 'tags',
      'placeholder' => '',
    ],
  ]);
  $definition_update_manager
    ->uninstallFieldStorageDefinition($storage_definition);
}

Functions

Namesort descending Description
commerce_pricelist_update_8201 Replace the 'customer_role' field with the 'customer_roles' field.
commerce_pricelist_update_8202 Define event handlers for price list and price list items.
commerce_pricelist_update_8203 Add a 'uid' field to price lists and price list items.
commerce_pricelist_update_8204 Fix the entity definition mismatch caused by the "data_table" key removal.
commerce_pricelist_update_8205 Replace the 'customer' field with the 'customers' field.