You are here

function commerce_pricelist_update_8205 in Commerce Pricelist 8.2

Replace the 'customer' field with the 'customers' field.

File

./commerce_pricelist.install, line 133
Install, update and uninstall functions for the Pricelist module.

Code

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