You are here

function commerce_wishlist_update_8309 in Commerce Wishlist 8.3

Convert the 'wishlist_items' configurable field into a base field.

File

./commerce_wishlist.install, line 215
Contains install and update functions for Commerce Wishlist.

Code

function commerce_wishlist_update_8309() {
  $bundle_info = \Drupal::service('entity_type.bundle.info')
    ->getBundleInfo('commerce_wishlist');
  $wishlist_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_wishlist.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_wishlist');
  $definitions['wishlist_items'] = $base_field_definitions['wishlist_items']
    ->getFieldStorageDefinition();
  $entity_definitions_installed
    ->set($key, $definitions);
  $configuration_to_delete = [];
  foreach ($wishlist_types as $bundle) {
    $configuration_to_delete[] = 'field.field.commerce_wishlist.' . $bundle . '.wishlist_items';
  }
  $configuration_to_delete[] = 'field.storage.commerce_wishlist.wishlist_items';
  $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();
  }
}