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.
18 class ModulesUninstallConfirmForm extends ConfirmFormBase {
19 use ConfigDependencyDeleteFormTrait;
22 * The module installer service.
24 * @var \Drupal\Core\Extension\ModuleInstallerInterface
26 protected $moduleInstaller;
29 * The expirable key value store.
31 * @var \Drupal\Core\KeyValueStore\KeyValueStoreExpirableInterface
33 protected $keyValueExpirable;
36 * The configuration manager.
38 * @var \Drupal\Core\Config\ConfigManagerInterface
40 protected $configManager;
45 * @var \Drupal\Core\Entity\EntityManagerInterface
47 protected $entityManager;
50 * An array of modules to uninstall.
54 protected $modules = [];
57 * Constructs a ModulesUninstallConfirmForm object.
59 * @param \Drupal\Core\Extension\ModuleInstallerInterface $module_installer
60 * The module installer.
61 * @param \Drupal\Core\KeyValueStore\KeyValueStoreExpirableInterface $key_value_expirable
62 * The key value expirable factory.
63 * @param \Drupal\Core\Config\ConfigManagerInterface $config_manager
64 * The configuration manager.
65 * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
68 public function __construct(ModuleInstallerInterface $module_installer, KeyValueStoreExpirableInterface $key_value_expirable, ConfigManagerInterface $config_manager, EntityManagerInterface $entity_manager) {
69 $this->moduleInstaller = $module_installer;
70 $this->keyValueExpirable = $key_value_expirable;
71 $this->configManager = $config_manager;
72 $this->entityManager = $entity_manager;
78 public static function create(ContainerInterface $container) {
80 $container->get('module_installer'),
81 $container->get('keyvalue.expirable')->get('modules_uninstall'),
82 $container->get('config.manager'),
83 $container->get('entity.manager')
90 public function getQuestion() {
91 return $this->t('Confirm uninstall');
97 public function getConfirmText() {
98 return $this->t('Uninstall');
104 public function getCancelUrl() {
105 return new Url('system.modules_uninstall');
111 public function getDescription() {
112 return $this->t('Would you like to continue with uninstalling the above?');
118 public function getFormId() {
119 return 'system_modules_uninstall_confirm_form';
125 public function buildForm(array $form, FormStateInterface $form_state) {
126 // Retrieve the list of modules from the key value store.
127 $account = $this->currentUser()->id();
128 $this->modules = $this->keyValueExpirable->get($account);
130 // Prevent this page from showing when the module list is empty.
131 if (empty($this->modules)) {
132 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');
133 return $this->redirect('system.modules_uninstall');
136 $data = system_rebuild_module_data();
137 $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>';
139 '#theme' => 'item_list',
140 '#items' => array_map(function ($module) use ($data) {
141 return $data[$module]->info['name'];
145 // List the dependent entities.
146 $this->addDependencyListsToForm($form, 'module', $this->modules, $this->configManager, $this->entityManager);
148 return parent::buildForm($form, $form_state);
154 public function submitForm(array &$form, FormStateInterface $form_state) {
155 // Clear the key value store entry.
156 $account = $this->currentUser()->id();
157 $this->keyValueExpirable->delete($account);
159 // Uninstall the modules.
160 $this->moduleInstaller->uninstall($this->modules);
162 drupal_set_message($this->t('The selected modules have been uninstalled.'));
163 $form_state->setRedirectUrl($this->getCancelUrl());