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.
18 class PrepareModulesEntityUninstallForm extends ConfirmFormBase {
21 * The entity type ID of the entities to delete.
25 protected $entityTypeId;
28 * The entity type manager.
30 * @var \Drupal\Core\Entity\EntityTypeManagerInterface
32 protected $entityTypeManager;
35 * Constructs a PrepareModulesEntityUninstallForm object.
37 * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
38 * The entity type manager.
40 public function __construct(EntityTypeManagerInterface $entity_type_manager) {
41 $this->entityTypeManager = $entity_type_manager;
47 public static function create(ContainerInterface $container) {
49 $container->get('entity_type.manager')
56 public function getFormId() {
57 return 'system_prepare_modules_entity_uninstall';
63 public function getQuestion() {
64 $entity_type = $this->entityTypeManager->getDefinition($this->entityTypeId);
66 return $this->t('Are you sure you want to delete all @entity_type_plural?', ['@entity_type_plural' => $entity_type->getPluralLabel()]);
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.');
79 public function getConfirmText() {
80 $entity_type = $this->entityTypeManager->getDefinition($this->entityTypeId);
82 return $this->t('Delete all @entity_type_plural', ['@entity_type_plural' => $entity_type->getPluralLabel()]);
88 public function getCancelUrl() {
89 return Url::fromRoute('system.modules_uninstall');
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();
100 $form = parent::buildForm($form, $form_state);
102 $storage = $this->entityTypeManager->getStorage($entity_type_id);
103 $count = $storage->getQuery()->count()->execute();
105 $form['entity_type_id'] = [
107 '#value' => $entity_type_id,
110 // Display a list of the 10 entity labels, if possible.
111 $entity_type = $this->entityTypeManager->getDefinition($entity_type_id);
114 '#markup' => $this->t(
115 'There are 0 @entity_type_plural to delete.',
116 ['@entity_type_plural' => $entity_type->getPluralLabel()]
120 elseif ($entity_type->hasKey('label')) {
121 $recent_entity_ids = $storage->getQuery()
122 ->sort($entity_type->getKey('id'), 'DESC')
125 $recent_entities = $storage->loadMultiple($recent_entity_ids);
128 foreach ($recent_entities as $entity) {
129 $labels[] = $entity->label();
133 $form['recent_entity_labels'] = [
134 '#theme' => 'item_list',
137 $more_count = $count - count($labels);
139 '#markup' => $this->formatPlural(
141 'And <strong>@count</strong> more @entity_type_singular.',
142 'And <strong>@count</strong> more @entity_type_plural.',
144 '@entity_type_singular' => $entity_type->getSingularLabel(),
145 '@entity_type_plural' => $entity_type->getPluralLabel(),
148 '#access' => (bool) $more_count,
154 '#markup' => $this->formatPlural(
156 'This will delete <strong>@count</strong> @entity_type_singular.',
157 'This will delete <strong>@count</strong> @entity_type_plural.',
159 '@entity_type_singular' => $entity_type->getSingularLabel(),
160 '@entity_type_plural' => $entity_type->getPluralLabel(),
166 $form['description']['#prefix'] = '<p>';
167 $form['description']['#suffix'] = '</p>';
168 $form['description']['#weight'] = 5;
170 // Only show the delete button if there are entities to delete.
171 $form['actions']['submit']['#access'] = (bool) $count;
179 public function submitForm(array &$form, FormStateInterface $form_state) {
180 $entity_type_id = $form_state->getValue('entity_type_id');
182 $entity_type_plural = $this->entityTypeManager->getDefinition($entity_type_id)->getPluralLabel();
184 'title' => t('Deleting @entity_type_plural', [
185 '@entity_type_plural' => $entity_type_plural,
189 [__CLASS__, 'deleteContentEntities'], [$entity_type_id],
192 'finished' => [__CLASS__, 'moduleBatchFinished'],
193 'progress_message' => '',
199 * Deletes the content entities of the specified entity type.
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.
207 * This batch callback is only meant to be used by this form.
209 public static function deleteContentEntities($entity_type_id, &$context) {
210 $storage = \Drupal::entityTypeManager()->getStorage($entity_type_id);
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;
216 if (!isset($context['sandbox']['progress'])) {
217 $context['sandbox']['progress'] = 0;
218 $context['sandbox']['max'] = $storage->getQuery()->count()->execute();
221 $entity_type = \Drupal::entityTypeManager()->getDefinition($entity_type_id);
222 $entity_ids = $storage->getQuery()
223 ->sort($entity_type->getKey('id'), 'ASC')
226 if ($entities = $storage->loadMultiple($entity_ids)) {
227 $storage->delete($entities);
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();
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']]);
241 $context['finished'] = 1;
246 * Implements callback_batch_finished().
248 * Finishes the module batch, redirect to the uninstall page and output the
249 * successful data deletion message.
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]));
255 return new RedirectResponse(Url::fromRoute('system.modules_uninstall')->setAbsolute()->toString());