3 namespace Drupal\system\Form;
5 use Drupal\Core\Config\ConfigManagerInterface;
6 use Drupal\Core\Config\Entity\ConfigDependencyDeleteFormTrait;
7 use Drupal\Core\Entity\EntityManagerInterface;
8 use Drupal\Core\Extension\ModuleInstallerInterface;
9 use Drupal\Core\Form\ConfirmFormBase;
10 use Drupal\Core\Form\FormStateInterface;
12 use Symfony\Component\DependencyInjection\ContainerInterface;
13 use Drupal\Core\KeyValueStore\KeyValueStoreExpirableInterface;
16 * Builds a confirmation form to uninstall selected modules.
20 class ModulesUninstallConfirmForm extends ConfirmFormBase {
21 use ConfigDependencyDeleteFormTrait;
24 * The module installer service.
26 * @var \Drupal\Core\Extension\ModuleInstallerInterface
28 protected $moduleInstaller;
31 * The expirable key value store.
33 * @var \Drupal\Core\KeyValueStore\KeyValueStoreExpirableInterface
35 protected $keyValueExpirable;
38 * The configuration manager.
40 * @var \Drupal\Core\Config\ConfigManagerInterface
42 protected $configManager;
47 * @var \Drupal\Core\Entity\EntityManagerInterface
49 protected $entityManager;
52 * An array of modules to uninstall.
56 protected $modules = [];
59 * Constructs a ModulesUninstallConfirmForm object.
61 * @param \Drupal\Core\Extension\ModuleInstallerInterface $module_installer
62 * The module installer.
63 * @param \Drupal\Core\KeyValueStore\KeyValueStoreExpirableInterface $key_value_expirable
64 * The key value expirable factory.
65 * @param \Drupal\Core\Config\ConfigManagerInterface $config_manager
66 * The configuration manager.
67 * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
70 public function __construct(ModuleInstallerInterface $module_installer, KeyValueStoreExpirableInterface $key_value_expirable, ConfigManagerInterface $config_manager, EntityManagerInterface $entity_manager) {
71 $this->moduleInstaller = $module_installer;
72 $this->keyValueExpirable = $key_value_expirable;
73 $this->configManager = $config_manager;
74 $this->entityManager = $entity_manager;
80 public static function create(ContainerInterface $container) {
82 $container->get('module_installer'),
83 $container->get('keyvalue.expirable')->get('modules_uninstall'),
84 $container->get('config.manager'),
85 $container->get('entity.manager')
92 public function getQuestion() {
93 return $this->t('Confirm uninstall');
99 public function getConfirmText() {
100 return $this->t('Uninstall');
106 public function getCancelUrl() {
107 return new Url('system.modules_uninstall');
113 public function getDescription() {
114 return $this->t('Would you like to continue with uninstalling the above?');
120 public function getFormId() {
121 return 'system_modules_uninstall_confirm_form';
127 public function buildForm(array $form, FormStateInterface $form_state) {
128 // Retrieve the list of modules from the key value store.
129 $account = $this->currentUser()->id();
130 $this->modules = $this->keyValueExpirable->get($account);
132 // Prevent this page from showing when the module list is empty.
133 if (empty($this->modules)) {
134 drupal_set_message($this->t('The selected modules could not be uninstalled, either due to a website problem or due to the uninstall confirmation form timing out. Please try again.'), 'error');
135 return $this->redirect('system.modules_uninstall');
138 $data = system_rebuild_module_data();
139 $form['text']['#markup'] = '<p>' . $this->t('The following modules will be completely uninstalled from your site, and <em>all data from these modules will be lost</em>!') . '</p>';
141 '#theme' => 'item_list',
142 '#items' => array_map(function ($module) use ($data) {
143 return $data[$module]->info['name'];
147 // List the dependent entities.
148 $this->addDependencyListsToForm($form, 'module', $this->modules, $this->configManager, $this->entityManager);
150 return parent::buildForm($form, $form_state);
156 public function submitForm(array &$form, FormStateInterface $form_state) {
157 // Clear the key value store entry.
158 $account = $this->currentUser()->id();
159 $this->keyValueExpirable->delete($account);
161 // Uninstall the modules.
162 $this->moduleInstaller->uninstall($this->modules);
164 drupal_set_message($this->t('The selected modules have been uninstalled.'));
165 $form_state->setRedirectUrl($this->getCancelUrl());