079e574ed8acfff735e9c6d4741922767968870b
[yaffs-website] / web / core / modules / system / src / Form / PrepareModulesEntityUninstallForm.php
1 <?php
2
3 namespace Drupal\system\Form;
4
5 use Drupal\Core\Entity\EntityTypeManagerInterface;
6 use Drupal\Core\Form\ConfirmFormBase;
7 use Drupal\Core\Form\FormStateInterface;
8 use Drupal\Core\Url;
9 use Symfony\Component\DependencyInjection\ContainerInterface;
10 use Symfony\Component\HttpFoundation\RedirectResponse;
11 use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
12
13 /**
14  * Provides a form removing module content entities data before uninstallation.
15  *
16  * @internal
17  */
18 class PrepareModulesEntityUninstallForm extends ConfirmFormBase {
19
20   /**
21    * The entity type ID of the entities to delete.
22    *
23    * @var string
24    */
25   protected $entityTypeId;
26
27   /**
28    * The entity type manager.
29    *
30    * @var \Drupal\Core\Entity\EntityTypeManagerInterface
31    */
32   protected $entityTypeManager;
33
34   /**
35    * Constructs a PrepareModulesEntityUninstallForm object.
36    *
37    * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
38    *   The entity type manager.
39    */
40   public function __construct(EntityTypeManagerInterface $entity_type_manager) {
41     $this->entityTypeManager = $entity_type_manager;
42   }
43
44   /**
45    * {@inheritdoc}
46    */
47   public static function create(ContainerInterface $container) {
48     return new static(
49       $container->get('entity_type.manager')
50     );
51   }
52
53   /**
54    * {@inheritdoc}
55    */
56   public function getFormId() {
57     return 'system_prepare_modules_entity_uninstall';
58   }
59
60   /**
61    * {@inheritdoc}
62    */
63   public function getQuestion() {
64     $entity_type = $this->entityTypeManager->getDefinition($this->entityTypeId);
65
66     return $this->t('Are you sure you want to delete all @entity_type_plural?', ['@entity_type_plural' => $entity_type->getPluralLabel()]);
67   }
68
69   /**
70    * {@inheritdoc}
71    */
72   public function getDescription() {
73     return $this->t('This action cannot be undone.<br />Make a backup of your database if you want to be able to restore these items.');
74   }
75
76   /**
77    * {@inheritdoc}
78    */
79   public function getConfirmText() {
80     $entity_type = $this->entityTypeManager->getDefinition($this->entityTypeId);
81
82     return $this->t('Delete all @entity_type_plural', ['@entity_type_plural' => $entity_type->getPluralLabel()]);
83   }
84
85   /**
86    * {@inheritdoc}
87    */
88   public function getCancelUrl() {
89     return Url::fromRoute('system.modules_uninstall');
90   }
91
92   /**
93    * {@inheritdoc}
94    */
95   public function buildForm(array $form, FormStateInterface $form_state, $entity_type_id = NULL) {
96     $this->entityTypeId = $entity_type_id;
97     if (!$this->entityTypeManager->hasDefinition($this->entityTypeId)) {
98       throw new NotFoundHttpException();
99     }
100     $form = parent::buildForm($form, $form_state);
101
102     $storage = $this->entityTypeManager->getStorage($entity_type_id);
103     $count = $storage->getQuery()->count()->execute();
104
105     $form['entity_type_id'] = [
106       '#type' => 'value',
107       '#value' => $entity_type_id,
108     ];
109
110     // Display a list of the 10 entity labels, if possible.
111     $entity_type = $this->entityTypeManager->getDefinition($entity_type_id);
112     if ($count == 0) {
113       $form['total'] = [
114         '#markup' => $this->t(
115           'There are 0 @entity_type_plural to delete.',
116           ['@entity_type_plural' => $entity_type->getPluralLabel()]
117         ),
118       ];
119     }
120     elseif ($entity_type->hasKey('label')) {
121       $recent_entity_ids = $storage->getQuery()
122         ->sort($entity_type->getKey('id'), 'DESC')
123         ->pager(10)
124         ->execute();
125       $recent_entities = $storage->loadMultiple($recent_entity_ids);
126
127       $labels = [];
128       foreach ($recent_entities as $entity) {
129         $labels[] = $entity->label();
130       }
131
132       if ($labels) {
133         $form['recent_entity_labels'] = [
134           '#theme' => 'item_list',
135           '#items' => $labels,
136         ];
137         $more_count = $count - count($labels);
138         $form['total'] = [
139           '#markup' => $this->formatPlural(
140             $more_count,
141             'And <strong>@count</strong> more @entity_type_singular.',
142             'And <strong>@count</strong> more @entity_type_plural.',
143             [
144               '@entity_type_singular' => $entity_type->getSingularLabel(),
145               '@entity_type_plural' => $entity_type->getPluralLabel(),
146             ]
147           ),
148           '#access' => (bool) $more_count,
149         ];
150       }
151     }
152     else {
153       $form['total'] = [
154         '#markup' => $this->formatPlural(
155           $count,
156           'This will delete <strong>@count</strong> @entity_type_singular.',
157           'This will delete <strong>@count</strong> @entity_type_plural.',
158           [
159             '@entity_type_singular' => $entity_type->getSingularLabel(),
160             '@entity_type_plural' => $entity_type->getPluralLabel(),
161           ]
162         )
163       ];
164     }
165
166     $form['description']['#prefix'] = '<p>';
167     $form['description']['#suffix'] = '</p>';
168     $form['description']['#weight'] = 5;
169
170     // Only show the delete button if there are entities to delete.
171     $form['actions']['submit']['#access'] = (bool) $count;
172
173     return $form;
174   }
175
176   /**
177    * {@inheritdoc}
178    */
179   public function submitForm(array &$form, FormStateInterface $form_state) {
180     $entity_type_id = $form_state->getValue('entity_type_id');
181
182     $entity_type_plural = $this->entityTypeManager->getDefinition($entity_type_id)->getPluralLabel();
183     $batch = [
184       'title' => t('Deleting @entity_type_plural', [
185         '@entity_type_plural' => $entity_type_plural,
186       ]),
187       'operations' => [
188         [
189           [__CLASS__, 'deleteContentEntities'], [$entity_type_id],
190         ],
191       ],
192       'finished' => [__CLASS__, 'moduleBatchFinished'],
193       'progress_message' => '',
194     ];
195     batch_set($batch);
196   }
197
198   /**
199    * Deletes the content entities of the specified entity type.
200    *
201    * @param string $entity_type_id
202    *   The entity type ID from which data will be deleted.
203    * @param array|\ArrayAccess $context
204    *   The batch context array, passed by reference.
205    *
206    * @internal
207    *   This batch callback is only meant to be used by this form.
208    */
209   public static function deleteContentEntities($entity_type_id, &$context) {
210     $storage = \Drupal::entityTypeManager()->getStorage($entity_type_id);
211
212     // Set the entity type ID in the results array so we can access it in the
213     // batch finished callback.
214     $context['results']['entity_type_id'] = $entity_type_id;
215
216     if (!isset($context['sandbox']['progress'])) {
217       $context['sandbox']['progress'] = 0;
218       $context['sandbox']['max'] = $storage->getQuery()->count()->execute();
219     }
220
221     $entity_type = \Drupal::entityTypeManager()->getDefinition($entity_type_id);
222     $entity_ids = $storage->getQuery()
223       ->sort($entity_type->getKey('id'), 'ASC')
224       ->range(0, 10)
225       ->execute();
226     if ($entities = $storage->loadMultiple($entity_ids)) {
227       $storage->delete($entities);
228     }
229     // Sometimes deletes cause secondary deletes. For example, deleting a
230     // taxonomy term can cause its children to be be deleted too.
231     $context['sandbox']['progress'] = $context['sandbox']['max'] - $storage->getQuery()->count()->execute();
232
233     // Inform the batch engine that we are not finished and provide an
234     // estimation of the completion level we reached.
235     if (count($entity_ids) > 0 && $context['sandbox']['progress'] != $context['sandbox']['max']) {
236       $context['finished'] = $context['sandbox']['progress'] / $context['sandbox']['max'];
237       $context['message'] = t('Deleting items... Completed @percentage% (@current of @total).', ['@percentage' => round(100 * $context['sandbox']['progress'] / $context['sandbox']['max']), '@current' => $context['sandbox']['progress'], '@total' => $context['sandbox']['max']]);
238
239     }
240     else {
241       $context['finished'] = 1;
242     }
243   }
244
245   /**
246    * Implements callback_batch_finished().
247    *
248    * Finishes the module batch, redirect to the uninstall page and output the
249    * successful data deletion message.
250    */
251   public static function moduleBatchFinished($success, $results, $operations) {
252     $entity_type_plural = \Drupal::entityTypeManager()->getDefinition($results['entity_type_id'])->getPluralLabel();
253     drupal_set_message(t('All @entity_type_plural have been deleted.', ['@entity_type_plural' => $entity_type_plural]));
254
255     return new RedirectResponse(Url::fromRoute('system.modules_uninstall')->setAbsolute()->toString());
256   }
257
258 }