You are here

public function BundlePluginInstaller::uninstallBundles in farmOS 2.x

File

modules/core/entity/src/BundlePlugin/BundlePluginInstaller.php, line 21

Class

BundlePluginInstaller
Extends the entity BundlePluginInstaller service.

Namespace

Drupal\farm_entity\BundlePlugin

Code

public function uninstallBundles(EntityTypeInterface $entity_type, array $modules) {
  $bundle_handler = $this->entityTypeManager
    ->getHandler($entity_type
    ->id(), 'bundle_plugin');
  $bundles = array_filter($bundle_handler
    ->getBundleInfo(), function ($bundle_info) use ($modules) {
    return in_array($bundle_info['provider'], $modules, TRUE);
  });

  /**
   * We need to uninstall the field storage definitions in a separate loop.
   *
   * This way we can allow a module to re-use the same field within multiple
   * bundles, allowing e.g to subclass a bundle plugin.
   *
   * @var \Drupal\entity\BundleFieldDefinition[] $field_storage_definitions
   */
  $field_storage_definitions = [];

  // Field definitions that should persist after uninstalling these bundles.
  $field_definitions_to_persist = $this
    ->getFieldDefinitionsToPersist($entity_type, array_keys($bundles));
  foreach (array_keys($bundles) as $bundle) {
    $this->entityBundleListener
      ->onBundleDelete($bundle, $entity_type
      ->id());
    foreach ($bundle_handler
      ->getFieldDefinitions($bundle) as $definition) {
      $field_name = $definition
        ->getName();
      $this->fieldDefinitionListener
        ->onFieldDefinitionDelete($definition);

      // Delete the field storage definition if it should not persist.
      if (!in_array($field_name, array_keys($field_definitions_to_persist))) {
        $field_storage_definitions[$field_name] = $definition;
      }
    }
  }
  foreach ($field_storage_definitions as $definition) {
    $this->fieldStorageDefinitionListener
      ->onFieldStorageDefinitionDelete($definition);
  }
}