function PrepareModulesEntityUninstallForm::buildForm

Same name in other branches
  1. 9 core/modules/system/src/Form/PrepareModulesEntityUninstallForm.php \Drupal\system\Form\PrepareModulesEntityUninstallForm::buildForm()
  2. 8.9.x core/modules/system/src/Form/PrepareModulesEntityUninstallForm.php \Drupal\system\Form\PrepareModulesEntityUninstallForm::buildForm()
  3. 10 core/modules/system/src/Form/PrepareModulesEntityUninstallForm.php \Drupal\system\Form\PrepareModulesEntityUninstallForm::buildForm()

Overrides ConfirmFormBase::buildForm

File

core/modules/system/src/Form/PrepareModulesEntityUninstallForm.php, line 129

Class

PrepareModulesEntityUninstallForm
Provides a form removing module content entities data before uninstallation.

Namespace

Drupal\system\Form

Code

public function buildForm(array $form, FormStateInterface $form_state, $entity_type_id = NULL) {
    $this->entityTypeId = $entity_type_id;
    if (!$this->entityTypeManager
        ->hasDefinition($this->entityTypeId)) {
        throw new NotFoundHttpException();
    }
    $form = parent::buildForm($form, $form_state);
    $storage = $this->entityTypeManager
        ->getStorage($entity_type_id);
    $count = $storage->getQuery()
        ->accessCheck(FALSE)
        ->count()
        ->execute();
    $accessible_count = $storage->getQuery()
        ->accessCheck(TRUE)
        ->count()
        ->execute();
    $form['entity_type_id'] = [
        '#type' => 'value',
        '#value' => $entity_type_id,
    ];
    // Display a list of the 10 entity labels, if possible.
    $entity_type = $this->entityTypeManager
        ->getDefinition($entity_type_id);
    if ($count == 0) {
        $form['total'] = [
            '#markup' => $this->t('There are 0 @entity_type_plural to delete.', [
                '@entity_type_plural' => $entity_type->getPluralLabel(),
            ]),
        ];
    }
    elseif ($accessible_count > 0 && $entity_type->hasKey('label')) {
        $recent_entity_ids = $storage->getQuery()
            ->accessCheck(TRUE)
            ->sort($entity_type->getKey('id'), 'DESC')
            ->pager(10)
            ->execute();
        $recent_entities = $storage->loadMultiple($recent_entity_ids);
        $labels = [];
        foreach ($recent_entities as $entity) {
            $labels[] = $entity->label();
        }
        if ($labels) {
            $form['recent_entity_labels'] = [
                '#theme' => 'item_list',
                '#items' => $labels,
            ];
            $more_count = $count - count($labels);
            $form['total'] = [
                '#markup' => $this->formatPlural($more_count, 'And <strong>@count</strong> more @entity_type_singular.', 'And <strong>@count</strong> more @entity_type_plural.', [
                    '@entity_type_singular' => $entity_type->getSingularLabel(),
                    '@entity_type_plural' => $entity_type->getPluralLabel(),
                ]),
                '#access' => (bool) $more_count,
            ];
        }
    }
    else {
        $form['total'] = [
            '#markup' => $this->formatPlural($count, 'This will delete <strong>@count</strong> @entity_type_singular.', 'This will delete <strong>@count</strong> @entity_type_plural.', [
                '@entity_type_singular' => $entity_type->getSingularLabel(),
                '@entity_type_plural' => $entity_type->getPluralLabel(),
            ]),
        ];
    }
    $form['description']['#prefix'] = '<p>';
    $form['description']['#suffix'] = '</p>';
    $form['description']['#weight'] = 5;
    // Only show the delete button if there are entities to delete.
    $form['actions']['submit']['#access'] = (bool) $count;
    return $form;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.