function commerce_wishlist_update_8304 in Commerce Wishlist 8.3
Replace the commerce_wishlist_item_type entity type with automatic bundles.
File
- ./
commerce_wishlist.install, line 84 - Contains install and update functions for Commerce Wishlist.
Code
function commerce_wishlist_update_8304() {
/** @var \Drupal\Core\Entity\EntityFieldManagerInterface $entity_field_manager */
$entity_field_manager = \Drupal::service('entity_field.manager');
/** @var \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager */
$entity_type_manager = \Drupal::service('entity_type.manager');
/** @var \Drupal\Core\Config\ConfigFactoryInterface $config_factory */
$config_factory = \Drupal::service('config.factory');
/** @var \Drupal\Core\Config\ConfigManagerInterface $config_manager */
$config_manager = \Drupal::service('config.manager');
// Start from empty caches, so that they reflect the updated code.
$entity_type_manager
->clearCachedDefinitions();
$entity_field_manager
->clearCachedFieldDefinitions();
// Remove all wishlist item types and their fields, form/view displays.
$names = $config_factory
->listAll('commerce_wishlist.commerce_wishlist_item_type.');
$dependents = $config_manager
->findConfigEntityDependents('config', $names);
foreach ($dependents as $dependent) {
$config_factory
->getEditable($dependent
->getConfigDependencyName());
}
foreach ($names as $name) {
$config_factory
->getEditable($name)
->delete();
}
// Update core's field schema and storage definition repositories.
$new_definitions = $entity_field_manager
->getBaseFieldDefinitions('commerce_wishlist_item');
$definition = $new_definitions['type']
->getFieldStorageDefinition();
$field_schema = $definition
->getSchema();
/** @var \Drupal\Core\Entity\EntityLastInstalledSchemaRepositoryInterface $last_installed_schema_repository */
$last_installed_schema_repository = \Drupal::service('entity.last_installed_schema.repository');
$last_installed_schema_repository
->setLastInstalledFieldStorageDefinition($definition);
$entity_storage_schema_sql = \Drupal::keyValue('entity.storage_schema.sql');
$schema_key = 'commerce_wishlist_item.field_schema_data.type';
$installed_field_schema = $entity_storage_schema_sql
->get($schema_key);
$installed_field_schema['commerce_wishlist_item']['fields']['type'] = $field_schema['columns']['value'];
$installed_field_schema['commerce_wishlist_item']['fields']['type']['not null'] = TRUE;
unset($installed_field_schema['commerce_wishlist_item']['indexes']);
$entity_storage_schema_sql
->set('commerce_wishlist_item.field_schema_data.type', $installed_field_schema);
// Alter the database table to reflect the new structure.
// Core doesn't provide an index for string bundle keys right now.
$database = \Drupal::database();
$database
->schema()
->changeField('commerce_wishlist_item', 'type', 'type', $installed_field_schema['commerce_wishlist_item']['fields']['type']);
$database
->schema()
->dropIndex('commerce_wishlist_item', 'commerce_wishlist_item_field__type__target_id');
// Update wishlist item storage.
// Assume all previous bundles were product variation bundles.
$database
->update('commerce_wishlist_item')
->fields([
'type' => 'commerce_product_variation',
])
->execute();
}