BundlePluginInstaller.php in farmOS 2.x
File
modules/core/entity/src/BundlePlugin/BundlePluginInstaller.php
View source
<?php
namespace Drupal\farm_entity\BundlePlugin;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\entity\BundlePlugin\BundlePluginInstaller as EntityBundlePluginInstaller;
class BundlePluginInstaller extends EntityBundlePluginInstaller {
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);
});
$field_storage_definitions = [];
$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);
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);
}
}
protected function getFieldDefinitionsToPersist(EntityTypeInterface $entity_type, array $uninstalled_bundles) {
$bundle_handler = $this->entityTypeManager
->getHandler($entity_type
->id(), 'bundle_plugin');
$remaining_bundles = array_filter($bundle_handler
->getBundleInfo(), function ($bundle_name) use ($uninstalled_bundles) {
return !in_array($bundle_name, $uninstalled_bundles, TRUE);
}, ARRAY_FILTER_USE_KEY);
$fields_to_persist = [];
foreach (array_keys($remaining_bundles) as $bundle) {
foreach ($bundle_handler
->getFieldDefinitions($bundle) as $definition) {
$field_name = $definition
->getName();
if (!isset($fields_to_persist[$field_name])) {
$fields_to_persist[$field_name] = $definition;
}
}
}
return $fields_to_persist;
}
}