diff --git a/core/core.services.yml b/core/core.services.yml index 6954b56..72fee69 100644 --- a/core/core.services.yml +++ b/core/core.services.yml @@ -291,7 +291,7 @@ services: arguments: ['@app.root', '%container.modules%', '@cache.bootstrap'] module_installer: class: Drupal\Core\Extension\ModuleInstaller - arguments: ['@app.root', '@module_handler', '@kernel'] + arguments: ['@app.root', '@module_handler', '@kernel', '@entity.manager', '@entity.query'] theme_handler: class: Drupal\Core\Extension\ThemeHandler arguments: ['@app.root', '@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 f074ed3..e7955ef 100644 --- a/core/lib/Drupal/Core/Extension/ModuleInstaller.php +++ b/core/lib/Drupal/Core/Extension/ModuleInstaller.php @@ -10,6 +10,8 @@ use Drupal\Component\Serialization\Yaml; use Drupal\Core\Cache\CacheBackendInterface; use Drupal\Core\DrupalKernelInterface; +use Drupal\Core\Entity\EntityManagerInterface; +use Drupal\Core\Entity\Query\QueryFactory; /** * Default implementation of the module installer. @@ -41,6 +43,27 @@ class ModuleInstaller implements ModuleInstallerInterface { protected $root; /** + * The entity manager. + * + * @var \Drupal\Core\Entity\EntityManagerInterface + */ + protected $entityManager; + + /** + * The entity query factory. + * + * @var \Drupal\Core\Entity\Query\QueryFactory + */ + protected $queryFactory; + + /** + * Modules that can be uninstalled. + * + * @var array + */ + protected $uninstallValidated; + + /** * Constructs a new ModuleInstaller instance. * * @param string $root @@ -49,14 +72,20 @@ class ModuleInstaller implements ModuleInstallerInterface { * The module handler. * @param \Drupal\Core\DrupalKernelInterface $kernel * The drupal kernel. + * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager + * The entity manager. + * @param \Drupal\Core\Entity\Query\QueryFactory $entity_query + * The entity query factory. * * @see \Drupal\Core\DrupalKernel * @see \Drupal\Core\CoreServiceProvider */ - public function __construct($root, ModuleHandlerInterface $module_handler, DrupalKernelInterface $kernel) { + public function __construct($root, ModuleHandlerInterface $module_handler, DrupalKernelInterface $kernel, EntityManagerInterface $entity_manager, QueryFactory $entity_query) { $this->root = $root; $this->moduleHandler = $module_handler; $this->kernel = $kernel; + $this->entityManager = $entity_manager; + $this->entityQueryFactory = $entity_query; } /** @@ -270,6 +299,7 @@ public function install(array $module_list, $enable_dependencies = TRUE) { * {@inheritdoc} */ public function uninstall(array $module_list, $uninstall_dependents = TRUE) { + $module_list = $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(); @@ -460,4 +490,47 @@ protected function updateKernel($module_filenames) { $this->moduleHandler = $container->get('module_handler'); } + /** + * {@inheritdoc} + */ + public function validateUninstall(array $module_list) { + foreach ($module_list as $module) { + if (isset($this->uninstallValidated[$module])) { + continue; + } + if ($this->moduleHasContent($module)) { + unset($module_list[$module]); + } + else { + $this->uninstallValidated[] = $module; + } + } + return $module_list; + } + + /** + * Determines if a module has existing content entities. + * + * @param string $module + * A module name. + * + * @return bool + * TRUE if there are content entities. + */ + protected function moduleHasContent($module) { + $entities = $this->entityManager->getDefinitions(); + foreach ($entities as $entity_type) { + if ($module == $entity_type->getProvider() && is_subclass_of($entity_type->getClass(), 'Drupal\Core\Entity\ContentEntityInterface')) { + $content = $this->queryFactory->get($entity_type->id()) + ->accessCheck(FALSE) + ->range(0, 1) + ->execute(); + if (!empty($content)) { + return TRUE; + } + } + } + return FALSE; + } + } diff --git a/core/lib/Drupal/Core/Extension/ModuleInstallerInterface.php b/core/lib/Drupal/Core/Extension/ModuleInstallerInterface.php index 246af4b..b11bc3e 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(array $module_list); + } diff --git a/core/modules/system/src/Form/ModulesUninstallForm.php b/core/modules/system/src/Form/ModulesUninstallForm.php index 4285956..d969d90 100644 --- a/core/modules/system/src/Form/ModulesUninstallForm.php +++ b/core/modules/system/src/Form/ModulesUninstallForm.php @@ -8,6 +8,7 @@ namespace Drupal\system\Form; use Drupal\Core\Extension\ModuleHandlerInterface; +use Drupal\Core\Extension\ModuleInstallerInterface; use Drupal\Core\Form\FormBase; use Drupal\Core\Form\FormStateInterface; use Drupal\Core\KeyValueStore\KeyValueStoreExpirableInterface; @@ -26,6 +27,13 @@ class ModulesUninstallForm extends FormBase { protected $moduleHandler; /** + * The module installer service. + * + * @var \Drupal\Core\Extension\ModuleInstallerInterface + */ + protected $moduleInstaller; + + /** * The expirable key value store. * * @var \Drupal\Core\KeyValueStore\KeyValueStoreExpirableInterface @@ -38,6 +46,7 @@ class ModulesUninstallForm extends FormBase { public static function create(ContainerInterface $container) { return new static( $container->get('module_handler'), + $container->get('module_installer'), $container->get('keyvalue.expirable')->get('modules_uninstall') ); } @@ -47,11 +56,14 @@ public static function create(ContainerInterface $container) { * * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler * The module handler. + * @param \Drupal\Core\Extension\ModuleInstallerInterface $module_installer + * The module installer. * @param \Drupal\Core\KeyValueStore\KeyValueStoreExpirableInterface $key_value_expirable * The key value expirable factory. */ - public function __construct(ModuleHandlerInterface $module_handler, KeyValueStoreExpirableInterface $key_value_expirable) { + public function __construct(ModuleHandlerInterface $module_handler, ModuleInstallerInterface $module_installer, KeyValueStoreExpirableInterface $key_value_expirable) { $this->moduleHandler = $module_handler; + $this->moduleInstaller = $module_installer; $this->keyValueExpirable = $key_value_expirable; } @@ -72,6 +84,8 @@ public function buildForm(array $form, FormStateInterface $form_state) { // Get a list of disabled, installed modules. $modules = system_rebuild_module_data(); $uninstallable = array_filter($modules, function ($module) use ($modules) { + // @todo use validateUninstall here also. + //$modules = $this->moduleInstaller->validateUninstall($modules); return empty($modules[$module->getName()]->info['required']) && drupal_get_installed_schema_version($module->getName()) > SCHEMA_UNINSTALLED; });