diff --git a/core/core.services.yml b/core/core.services.yml index 3e2feb6..58270f4 100644 --- a/core/core.services.yml +++ b/core/core.services.yml @@ -289,7 +289,14 @@ services: arguments: ['@app.root', '%container.modules%', '@cache.bootstrap'] module_installer: class: Drupal\Core\Extension\ModuleInstaller + tags: + - { name: service_collector, tag: 'module_install.uninstall_validator', call: addUninstallValidator } arguments: ['@app.root', '@module_handler', '@kernel'] + content_uninstall_validator: + class: Drupal\Core\Entity\ContentUninstallValidator + tags: + - { name: module_install.uninstall_validator } + arguments: ['@entity.manager'] 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/Entity/ContentUninstallValidator.php b/core/lib/Drupal/Core/Entity/ContentUninstallValidator.php new file mode 100644 index 0000000..359f158 --- /dev/null +++ b/core/lib/Drupal/Core/Entity/ContentUninstallValidator.php @@ -0,0 +1,46 @@ +entityManager = $entity_manager; + } + + /** + * {@inheritdoc} + */ + public function validate($module) { + $entity_types = $this->entityManager->getDefinitions(); + $reasons = array(); + foreach ($entity_types as $entity_type) { + if ($module == $entity_type->getProvider() && $entity_type instanceof ContentEntityTypeInterface && $this->entityManager->getStorage($entity_type->id())->hasData()) { + $reasons[] = $this->t('There is content for the entity type: @entity_type', array('@entity_type' => $entity_type->getLabel())); + } + } + if ($reasons) { + return $reasons; + } + return NULL; + } + +} diff --git a/core/lib/Drupal/Core/Extension/ModuleInstaller.php b/core/lib/Drupal/Core/Extension/ModuleInstaller.php index 20a6506..9149ce9 100644 --- a/core/lib/Drupal/Core/Extension/ModuleInstaller.php +++ b/core/lib/Drupal/Core/Extension/ModuleInstaller.php @@ -11,6 +11,7 @@ use Drupal\Core\Cache\Cache; use Drupal\Core\Cache\CacheBackendInterface; use Drupal\Core\DrupalKernelInterface; +use Drupal\Core\Extension\UninstallValidatorInterface; /** * Default implementation of the module installer. @@ -42,6 +43,13 @@ class ModuleInstaller implements ModuleInstallerInterface { protected $root; /** + * The uninstall validators. + * + * @var \Drupal\Core\Extension\UninstallValidatorInterface[] + */ + protected $uninstallValidators; + + /** * Constructs a new ModuleInstaller instance. * * @param string $root @@ -63,6 +71,13 @@ public function __construct($root, ModuleHandlerInterface $module_handler, Drupa /** * {@inheritdoc} */ + public function addUninstallValidator(UninstallValidatorInterface $uninstall_validator) { + $this->uninstallValidators[] = $uninstall_validator; + } + + /** + * {@inheritdoc} + */ public function install(array $module_list, $enable_dependencies = TRUE) { $extension_config = \Drupal::config('core.extension'); if ($enable_dependencies) { @@ -306,6 +321,14 @@ public function uninstall(array $module_list, $uninstall_dependents = TRUE) { } } + // Use the validators and print the reason + if ($reasons = $this->validateUninstall($module_list)) { + foreach ($reasons as $reason) { + drupal_set_message($reason, 'error'); + } + // The validation failed. + return FALSE; + } // Set the actual module weights. $module_list = array_map(function ($module) use ($module_data) { return $module_data[$module]->sort; @@ -315,7 +338,7 @@ public function uninstall(array $module_list, $uninstall_dependents = TRUE) { asort($module_list); $module_list = array_keys($module_list); - // Only process modules that are enabled. A module is only enabled if it is + // Only process modules that are enabled. A module is only disabled if it is // configured as enabled. Custom or overridden module handlers might contain // the module already, which means that it might be loaded, but not // necessarily installed. @@ -469,4 +492,23 @@ protected function updateKernel($module_filenames) { $this->moduleHandler = $container->get('module_handler'); } + /** + * {@inheritdoc} + */ + public function validateUninstall(array $module_list) { + $reasons = array(); + foreach ($module_list as $key => $module) { + foreach ($this->uninstallValidators as $validator) { + $validation_reasons = $validator->validate($module); + if ($validation_reasons !== NULL) { + $reasons = array_merge($reasons, $validation_reasons); + } + } + } + if ($reasons) { + return $reasons; + } + return NULL; + } + } diff --git a/core/lib/Drupal/Core/Extension/ModuleInstallerInterface.php b/core/lib/Drupal/Core/Extension/ModuleInstallerInterface.php index 246af4b..a64a9f5 100644 --- a/core/lib/Drupal/Core/Extension/ModuleInstallerInterface.php +++ b/core/lib/Drupal/Core/Extension/ModuleInstallerInterface.php @@ -7,6 +7,8 @@ namespace Drupal\Core\Extension; +use Drupal\Core\Extension\UninstallValidatorInterface; + /** * Provides the installation of modules with creating the db schema and more. */ @@ -58,5 +60,24 @@ public function install(array $module_list, $enable_dependencies = TRUE); */ public function uninstall(array $module_list, $uninstall_dependents = TRUE); + /** + * Adds module uninstall validator services. + * + * @param \Drupal\Core\Extension\UninstallValidatorInterface $uninstall_validator + * The uninstall validation service to add. + */ + public function addUninstallValidator(UninstallValidatorInterface $uninstall_validator); + + /** + * Determines whether a list of modules can be uninstalled. + * + * @param array $module_list + * The modules to uninstall. + * + * @return array + * Reasons for which the module list can not be uninstalled or NULL + */ + public function validateUninstall(array $module_list); + } diff --git a/core/lib/Drupal/Core/Extension/UninstallValidatorInterface.php b/core/lib/Drupal/Core/Extension/UninstallValidatorInterface.php new file mode 100644 index 0000000..748e7ab --- /dev/null +++ b/core/lib/Drupal/Core/Extension/UninstallValidatorInterface.php @@ -0,0 +1,24 @@ +delete(); } + // Delete any shortcuts so the shortcut module can be uninstalled. + $shortcuts = Shortcut::loadMultiple(); + entity_delete_multiple('shortcut', array_keys($shortcuts)); + system_list_reset(); $all_modules = system_rebuild_module_data(); diff --git a/core/modules/system/src/Form/ModulesUninstallForm.php b/core/modules/system/src/Form/ModulesUninstallForm.php index 4285956..e0a728a 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; } @@ -69,7 +81,7 @@ public function buildForm(array $form, FormStateInterface $form_state) { // Make sure the install API is available. include_once DRUPAL_ROOT . '/core/includes/install.inc'; - // Get a list of disabled, installed modules. + // Get a list all available modules. $modules = system_rebuild_module_data(); $uninstallable = array_filter($modules, function ($module) use ($modules) { return empty($modules[$module->getName()]->info['required']) && drupal_get_installed_schema_version($module->getName()) > SCHEMA_UNINSTALLED; @@ -112,7 +124,7 @@ public function buildForm(array $form, FormStateInterface $form_state) { uasort($uninstallable, 'system_sort_modules_by_info_name'); $form['uninstall'] = array('#tree' => TRUE); - foreach ($uninstallable as $module) { + foreach ($uninstallable as $module_key => $module) { $name = $module->info['name'] ?: $module->getName(); $form['modules'][$module->getName()]['#module_name'] = $name; $form['modules'][$module->getName()]['name']['#markup'] = $name; @@ -124,6 +136,12 @@ public function buildForm(array $form, FormStateInterface $form_state) { '#title_display' => 'invisible', ); + // If a validator returns reasons not to uninstall a module, + // list the reasons and disable the check box. + if ($reasons = $this->moduleInstaller->validateUninstall(array($module_key))) { + $form['modules'][$module->getName()]['#validation_reasons'] = $reasons; + $form['uninstall'][$module->getName()]['#disabled'] = TRUE; + } // All modules which depend on this one must be uninstalled first, before // we can allow this module to be uninstalled. (The installation profile // is excluded from this list.) diff --git a/core/modules/system/src/Tests/Extension/ModuleHandlerTest.php b/core/modules/system/src/Tests/Extension/ModuleHandlerTest.php index 83cd5fc..7aca3c0 100644 --- a/core/modules/system/src/Tests/Extension/ModuleHandlerTest.php +++ b/core/modules/system/src/Tests/Extension/ModuleHandlerTest.php @@ -200,6 +200,48 @@ function testUninstallProfileDependency() { } /** + * Tests uninstalling a module that has content. + */ + function testUninstallContentDependency() { + $this->enableModules(array('module_test', 'entity_test', 'text', 'user', 'help')); + $this->assertTrue($this->moduleHandler()->moduleExists('entity_test'), 'Test module is enabled.'); + $this->assertTrue($this->moduleHandler()->moduleExists('module_test'), 'Test module is enabled.'); + + $this->installSchema('user', 'users_data'); + $entity_types = \Drupal::entityManager()->getDefinitions(); + foreach ($entity_types as $entity_type) { + if ('entity_test' == $entity_type->getProvider()) { + $this->installEntitySchema($entity_type->id()); + } + } + + // Create a fake dependency. + // entity_test will depend on help. + \Drupal::state()->set('module_test.dependency', 'dependency'); + drupal_static_reset('system_rebuild_module_data'); + + // Create an entity so that the modules can not be disabled. + $entity = entity_create('entity_test', array('name' => $this->randomString())); + $entity->save(); + + $result = $this->moduleInstaller()->uninstall(array('entity_test')); + $this->assertFalse($result, 'ModuleHandler::uninstall() returns FALSE.'); + + // uninstalling help needs entity_test to be un-installable. + $result = $this->moduleInstaller()->uninstall(array('help')); + $this->assertFalse($result, 'ModuleHandler::uninstall() returns FALSE.'); + + // Deleting the entity. + $entity->delete(); + + $result = $this->moduleInstaller()->uninstall(array('entity_test')); + $this->assertTrue($result, 'ModuleHandler::uninstall() returns TRUE.'); + + $result = $this->moduleInstaller()->uninstall(array('help')); + $this->assertTrue($result, 'ModuleHandler::uninstall() returns TRUE.'); + } + + /** * Tests whether the correct module metadata is returned. */ function testModuleMetaData() { diff --git a/core/modules/system/system.admin.inc b/core/modules/system/system.admin.inc index 2ef1ea2..cbb5fe8 100644 --- a/core/modules/system/system.admin.inc +++ b/core/modules/system/system.admin.inc @@ -312,6 +312,12 @@ function theme_system_modules_uninstall($variables) { else { $disabled_message = ''; } + if (!empty($form['modules'][$module]['#validation_reasons'])) { + $disabled_message = format_plural(count($form['modules'][$module]['#validation_reasons']), + 'The following reason prevents @module from being uninstalled: @reasons', + 'The following reasons prevents @module from being uninstalled: @reasons', + array('@module' => $form['modules'][$module]['#module_name'], '@reasons' => implode(', ', $form['modules'][$module]['#validation_reasons']))); + } $rows[] = array( array('data' => drupal_render($form['uninstall'][$module]), 'align' => 'center'), array( diff --git a/core/modules/system/tests/modules/module_test/module_test.module b/core/modules/system/tests/modules/module_test/module_test.module index 60d53ef..92f038b 100644 --- a/core/modules/system/tests/modules/module_test/module_test.module +++ b/core/modules/system/tests/modules/module_test/module_test.module @@ -27,6 +27,10 @@ function module_test_system_info_alter(&$info, Extension $file, $type) { // Make config module depend on help module. $info['dependencies'][] = 'help'; } + elseif ($file->getName() == 'entity_test') { + // Make entity test module depend on help module. + $info['dependencies'][] = 'help'; + } } elseif (\Drupal::state()->get('module_test.dependency') == 'version dependency') { if ($file->getName() == 'color') {