function commerce_product_update_8208 in Commerce Core 8.2
Convert 'stores' and 'variations' configurable fields into base fields.
File
- modules/
product/ commerce_product.install, line 158 - Install, update and uninstall functions for the Product module.
Code
function commerce_product_update_8208() {
$bundle_info = \Drupal::service('entity_type.bundle.info')
->getBundleInfo('commerce_product');
$product_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_product.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_product');
$definitions['stores'] = $base_field_definitions['stores']
->getFieldStorageDefinition();
$definitions['variations'] = $base_field_definitions['variations']
->getFieldStorageDefinition();
$entity_definitions_installed
->set($key, $definitions);
$configuration_to_delete = [];
foreach ($product_types as $bundle) {
$configuration_to_delete[] = 'field.field.commerce_product.' . $bundle . '.stores';
$configuration_to_delete[] = 'field.field.commerce_product.' . $bundle . '.variations';
}
$configuration_to_delete[] = 'field.storage.commerce_product.stores';
$configuration_to_delete[] = 'field.storage.commerce_product.variations';
$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();
}
}