You are here

function commerce_log_update_8201 in Commerce Core 8.2

Add a "source_entity" index and reduce the "source_entity_type" maxlength.

File

modules/log/commerce_log.install, line 14
Install, update and uninstall functions for the commerce_log module.

Code

function commerce_log_update_8201() {
  $entity_type_manager = \Drupal::entityTypeManager();
  $entity_field_manager = \Drupal::service('entity_field.manager');
  $entity_field_manager
    ->clearCachedFieldDefinitions();
  $definition_update_manager = \Drupal::entityDefinitionUpdateManager();
  $database = \Drupal::database();

  // Resize the source_entity_type field.
  $database
    ->schema()
    ->changeField('commerce_log', 'source_entity_type', 'source_entity_type', [
    'type' => 'varchar',
    'length' => EntityTypeInterface::ID_MAX_LENGTH,
  ]);

  // Update storage schema.
  $storage_key = 'commerce_log.field_schema_data.source_entity_type';
  $storage_schema = \Drupal::keyValue('entity.storage_schema.sql');
  $field_schema = $storage_schema
    ->get($storage_key);
  $field_schema['commerce_log']['fields']['source_entity_type']['length'] = EntityTypeInterface::ID_MAX_LENGTH;
  $storage_schema
    ->set($storage_key, $field_schema);
  $key = 'commerce_log.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_log');
  $definitions['source_entity_type'] = $base_field_definitions['source_entity_type']
    ->getFieldStorageDefinition();
  $entity_definitions_installed
    ->set($key, $definitions);

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

  // Regenerate entity type indexes.
  $definition_update_manager
    ->updateEntityType($entity_type);
}