f55f0d69ec5a5b84329560cf6b4910bd4d59ef9a
[yaffs-website] / web / core / modules / system / src / Form / ModulesUninstallConfirmForm.php
1 <?php
2
3 namespace Drupal\system\Form;
4
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;
11 use Drupal\Core\Url;
12 use Symfony\Component\DependencyInjection\ContainerInterface;
13 use Drupal\Core\KeyValueStore\KeyValueStoreExpirableInterface;
14
15 /**
16  * Builds a confirmation form to uninstall selected modules.
17  *
18  * @internal
19  */
20 class ModulesUninstallConfirmForm extends ConfirmFormBase {
21   use ConfigDependencyDeleteFormTrait;
22
23   /**
24    * The module installer service.
25    *
26    * @var \Drupal\Core\Extension\ModuleInstallerInterface
27    */
28   protected $moduleInstaller;
29
30   /**
31    * The expirable key value store.
32    *
33    * @var \Drupal\Core\KeyValueStore\KeyValueStoreExpirableInterface
34    */
35   protected $keyValueExpirable;
36
37   /**
38    * The configuration manager.
39    *
40    * @var \Drupal\Core\Config\ConfigManagerInterface
41    */
42   protected $configManager;
43
44   /**
45    * The entity manager.
46    *
47    * @var \Drupal\Core\Entity\EntityManagerInterface
48    */
49   protected $entityManager;
50
51   /**
52    * An array of modules to uninstall.
53    *
54    * @var array
55    */
56   protected $modules = [];
57
58   /**
59    * Constructs a ModulesUninstallConfirmForm object.
60    *
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
68    *   The entity manager.
69    */
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;
75   }
76
77   /**
78    * {@inheritdoc}
79    */
80   public static function create(ContainerInterface $container) {
81     return new static(
82       $container->get('module_installer'),
83       $container->get('keyvalue.expirable')->get('modules_uninstall'),
84       $container->get('config.manager'),
85       $container->get('entity.manager')
86     );
87   }
88
89   /**
90    * {@inheritdoc}
91    */
92   public function getQuestion() {
93     return $this->t('Confirm uninstall');
94   }
95
96   /**
97    * {@inheritdoc}
98    */
99   public function getConfirmText() {
100     return $this->t('Uninstall');
101   }
102
103   /**
104    * {@inheritdoc}
105    */
106   public function getCancelUrl() {
107     return new Url('system.modules_uninstall');
108   }
109
110   /**
111    * {@inheritdoc}
112    */
113   public function getDescription() {
114     return $this->t('Would you like to continue with uninstalling the above?');
115   }
116
117   /**
118    * {@inheritdoc}
119    */
120   public function getFormId() {
121     return 'system_modules_uninstall_confirm_form';
122   }
123
124   /**
125    * {@inheritdoc}
126    */
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);
131
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');
136     }
137
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>';
140     $form['modules'] = [
141       '#theme' => 'item_list',
142       '#items' => array_map(function ($module) use ($data) {
143         return $data[$module]->info['name'];
144       }, $this->modules),
145     ];
146
147     // List the dependent entities.
148     $this->addDependencyListsToForm($form, 'module', $this->modules, $this->configManager, $this->entityManager);
149
150     return parent::buildForm($form, $form_state);
151   }
152
153   /**
154    * {@inheritdoc}
155    */
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);
160
161     // Uninstall the modules.
162     $this->moduleInstaller->uninstall($this->modules);
163
164     drupal_set_message($this->t('The selected modules have been uninstalled.'));
165     $form_state->setRedirectUrl($this->getCancelUrl());
166   }
167
168 }