diff --git a/core/core.services.yml b/core/core.services.yml index 1c898cb..9a5e8bf 100644 --- a/core/core.services.yml +++ b/core/core.services.yml @@ -290,7 +290,7 @@ services: arguments: ['%container.modules%', '@cache.bootstrap'] module_installer: class: Drupal\Core\Extension\ModuleInstaller - arguments: ['@module_handler', '@kernel'] + arguments: ['@module_handler', '@kernel', '@entity.manager'] theme_handler: class: Drupal\Core\Extension\ThemeHandler arguments: ['@config.factory', '@module_handler', '@state', '@info_parser', '@logger.channel.default', '@asset.css.collection_optimizer', '@config.installer', '@config.manager', '@router.builder_indicator'] diff --git a/core/lib/Drupal/Core/Extension/ModuleInstaller.php b/core/lib/Drupal/Core/Extension/ModuleInstaller.php index f5e3c2d..4d8578e 100644 --- a/core/lib/Drupal/Core/Extension/ModuleInstaller.php +++ b/core/lib/Drupal/Core/Extension/ModuleInstaller.php @@ -10,6 +10,7 @@ use Drupal\Component\Serialization\Yaml; use Drupal\Core\Cache\CacheBackendInterface; use Drupal\Core\DrupalKernelInterface; +use Drupal\Core\Entity\EntityManagerInterface; /** * Default implementation of the module installer. @@ -27,12 +28,24 @@ class ModuleInstaller implements ModuleInstallerInterface { protected $moduleHandler; /** + * @var \Drupal\Core\Entity\EntityManagerInterface + */ + protected $entityManager; + + /** + * Modules that can be uninstalled. + * @var array + */ + protected $uninstallValidated; + + /** * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler * The module handler. */ - public function __construct(ModuleHandlerInterface $module_handler, DrupalKernelInterface $kernel) { + public function __construct(ModuleHandlerInterface $module_handler, DrupalKernelInterface $kernel, EntityManagerInterface $entity_manager) { $this->moduleHandler = $module_handler; $this->kernel = $kernel; + $this->entityManager = $entity_manager; } /** @@ -246,6 +259,7 @@ public function install(array $module_list, $enable_dependencies = TRUE) { * {@inheritdoc} */ public function uninstall(array $module_list, $uninstall_dependents = TRUE) { + $this->validateUninstall($module_list); // Get all module data so we can find dependencies and sort. $module_data = system_rebuild_module_data(); $module_list = $module_list ? array_combine($module_list, $module_list) : array(); @@ -436,4 +450,18 @@ protected function updateKernel($module_filenames) { $this->moduleHandler = $container->get('module_handler'); } + /** + * {@inheritdoc} + */ + public function validateUninstall($module_list) { + foreach ($this->entityManager->getDefinitions() as $type => $entity_type) { + $entity_type->getProvider(); + // @todo: perform an entity query to see if any entities of this type + // exist. If they do and the entity type provider is in $module_list, + // remove it from $module_list and drupal_set_message an explanation of + // why the module cannot be uninstalled. + // Update $this->uninstallValidated with modules that can be removed. + // Refer to $this->uninstallValidated to speed this up. + } + } } diff --git a/core/lib/Drupal/Core/Extension/ModuleInstallerInterface.php b/core/lib/Drupal/Core/Extension/ModuleInstallerInterface.php index 246af4b..d2d5409 100644 --- a/core/lib/Drupal/Core/Extension/ModuleInstallerInterface.php +++ b/core/lib/Drupal/Core/Extension/ModuleInstallerInterface.php @@ -58,5 +58,13 @@ public function install(array $module_list, $enable_dependencies = TRUE); */ public function uninstall(array $module_list, $uninstall_dependents = TRUE); + /** + * Determines which modules can be uninstalled. + * + * @param array $module_list + * The modules to uninstall. + */ + public function validateUninstall($module_list); + }