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