You are here

public function PrepareModulesEntityUninstallForm::buildForm in Drupal 8

Same name and namespace in other branches
  1. 9 core/modules/system/src/Form/PrepareModulesEntityUninstallForm.php \Drupal\system\Form\PrepareModulesEntityUninstallForm::buildForm()

Form constructor.

Parameters

array $form: An associative array containing the structure of the form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

Return value

array The form structure.

Overrides ConfirmFormBase::buildForm

File

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

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()
    ->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 ($entity_type
    ->hasKey('label')) {
    $recent_entity_ids = $storage
      ->getQuery()
      ->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;
}