3 namespace Drupal\system\Form;
5 use Drupal\Core\Entity\EntityTypeManagerInterface;
6 use Drupal\Core\Form\ConfirmFormBase;
7 use Drupal\Core\Form\FormStateInterface;
9 use Symfony\Component\DependencyInjection\ContainerInterface;
10 use Symfony\Component\HttpFoundation\RedirectResponse;
11 use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
14 * Provides a form removing module content entities data before uninstallation.
16 class PrepareModulesEntityUninstallForm extends ConfirmFormBase {
19 * The entity type ID of the entities to delete.
23 protected $entityTypeId;
26 * The entity type manager.
28 * @var \Drupal\Core\Entity\EntityTypeManagerInterface
30 protected $entityTypeManager;
33 * Constructs a PrepareModulesEntityUninstallForm object.
35 * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
36 * The entity type manager.
38 public function __construct(EntityTypeManagerInterface $entity_type_manager) {
39 $this->entityTypeManager = $entity_type_manager;
45 public static function create(ContainerInterface $container) {
47 $container->get('entity_type.manager')
54 public function getFormId() {
55 return 'system_prepare_modules_entity_uninstall';
61 public function getQuestion() {
62 $entity_type = $this->entityTypeManager->getDefinition($this->entityTypeId);
64 return $this->t('Are you sure you want to delete all @entity_type_plural?', ['@entity_type_plural' => $entity_type->getPluralLabel()]);
70 public function getDescription() {
71 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.');
77 public function getConfirmText() {
78 $entity_type = $this->entityTypeManager->getDefinition($this->entityTypeId);
80 return $this->t('Delete all @entity_type_plural', ['@entity_type_plural' => $entity_type->getPluralLabel()]);
86 public function getCancelUrl() {
87 return Url::fromRoute('system.modules_uninstall');
93 public function buildForm(array $form, FormStateInterface $form_state, $entity_type_id = NULL) {
94 $this->entityTypeId = $entity_type_id;
95 if (!$this->entityTypeManager->hasDefinition($this->entityTypeId)) {
96 throw new NotFoundHttpException();
98 $form = parent::buildForm($form, $form_state);
100 $storage = $this->entityTypeManager->getStorage($entity_type_id);
101 $count = $storage->getQuery()->count()->execute();
103 $form['entity_type_id'] = [
105 '#value' => $entity_type_id,
108 // Display a list of the 10 entity labels, if possible.
109 $entity_type = $this->entityTypeManager->getDefinition($entity_type_id);
112 '#markup' => $this->t(
113 'There are 0 @entity_type_plural to delete.',
114 ['@entity_type_plural' => $entity_type->getPluralLabel()]
118 elseif ($entity_type->hasKey('label')) {
119 $recent_entity_ids = $storage->getQuery()
120 ->sort($entity_type->getKey('id'), 'DESC')
123 $recent_entities = $storage->loadMultiple($recent_entity_ids);
126 foreach ($recent_entities as $entity) {
127 $labels[] = $entity->label();
131 $form['recent_entity_labels'] = [
132 '#theme' => 'item_list',
135 $more_count = $count - count($labels);
137 '#markup' => $this->formatPlural(
139 'And <strong>@count</strong> more @entity_type_singular.',
140 'And <strong>@count</strong> more @entity_type_plural.',
142 '@entity_type_singular' => $entity_type->getSingularLabel(),
143 '@entity_type_plural' => $entity_type->getPluralLabel(),
146 '#access' => (bool) $more_count,
152 '#markup' => $this->formatPlural(
154 'This will delete <strong>@count</strong> @entity_type_singular.',
155 'This will delete <strong>@count</strong> @entity_type_plural.',
157 '@entity_type_singular' => $entity_type->getSingularLabel(),
158 '@entity_type_plural' => $entity_type->getPluralLabel(),
164 $form['description']['#prefix'] = '<p>';
165 $form['description']['#suffix'] = '</p>';
166 $form['description']['#weight'] = 5;
168 // Only show the delete button if there are entities to delete.
169 $form['actions']['submit']['#access'] = (bool) $count;
177 public function submitForm(array &$form, FormStateInterface $form_state) {
178 $entity_type_id = $form_state->getValue('entity_type_id');
180 $entity_type_plural = $this->entityTypeManager->getDefinition($entity_type_id)->getPluralLabel();
182 'title' => t('Deleting @entity_type_plural', [
183 '@entity_type_plural' => $entity_type_plural,
187 [__CLASS__, 'deleteContentEntities'], [$entity_type_id],
190 'finished' => [__CLASS__, 'moduleBatchFinished'],
191 'progress_message' => '',
197 * Deletes the content entities of the specified entity type.
199 * @param string $entity_type_id
200 * The entity type ID from which data will be deleted.
201 * @param array|\ArrayAccess $context
202 * The batch context array, passed by reference.
205 * This batch callback is only meant to be used by this form.
207 public static function deleteContentEntities($entity_type_id, &$context) {
208 $storage = \Drupal::entityTypeManager()->getStorage($entity_type_id);
210 // Set the entity type ID in the results array so we can access it in the
211 // batch finished callback.
212 $context['results']['entity_type_id'] = $entity_type_id;
214 if (!isset($context['sandbox']['progress'])) {
215 $context['sandbox']['progress'] = 0;
216 $context['sandbox']['max'] = $storage->getQuery()->count()->execute();
219 $entity_type = \Drupal::entityTypeManager()->getDefinition($entity_type_id);
220 $entity_ids = $storage->getQuery()
221 ->sort($entity_type->getKey('id'), 'ASC')
224 if ($entities = $storage->loadMultiple($entity_ids)) {
225 $storage->delete($entities);
227 // Sometimes deletes cause secondary deletes. For example, deleting a
228 // taxonomy term can cause it's children to be be deleted too.
229 $context['sandbox']['progress'] = $context['sandbox']['max'] - $storage->getQuery()->count()->execute();
231 // Inform the batch engine that we are not finished and provide an
232 // estimation of the completion level we reached.
233 if (count($entity_ids) > 0 && $context['sandbox']['progress'] != $context['sandbox']['max']) {
234 $context['finished'] = $context['sandbox']['progress'] / $context['sandbox']['max'];
235 $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']]);
239 $context['finished'] = 1;
244 * Implements callback_batch_finished().
246 * Finishes the module batch, redirect to the uninstall page and output the
247 * successful data deletion message.
249 public static function moduleBatchFinished($success, $results, $operations) {
250 $entity_type_plural = \Drupal::entityTypeManager()->getDefinition($results['entity_type_id'])->getPluralLabel();
251 drupal_set_message(t('All @entity_type_plural have been deleted.', ['@entity_type_plural' => $entity_type_plural]));
253 return new RedirectResponse(Url::fromRoute('system.modules_uninstall')->setAbsolute()->toString());