class PrepareModulesEntityUninstallForm
Same name in other branches
- 9 core/modules/system/src/Form/PrepareModulesEntityUninstallForm.php \Drupal\system\Form\PrepareModulesEntityUninstallForm
- 8.9.x core/modules/system/src/Form/PrepareModulesEntityUninstallForm.php \Drupal\system\Form\PrepareModulesEntityUninstallForm
- 11.x core/modules/system/src/Form/PrepareModulesEntityUninstallForm.php \Drupal\system\Form\PrepareModulesEntityUninstallForm
Provides a form removing module content entities data before uninstallation.
@internal
Hierarchy
- class \Drupal\Core\Form\FormBase implements \Drupal\Core\Form\FormInterface, \Drupal\Core\DependencyInjection\ContainerInjectionInterface uses \Drupal\Core\DependencyInjection\DependencySerializationTrait, \Drupal\Core\Logger\LoggerChannelTrait, \Drupal\Core\Messenger\MessengerTrait, \Drupal\Core\Routing\RedirectDestinationTrait, \Drupal\Core\StringTranslation\StringTranslationTrait
- class \Drupal\Core\Form\ConfirmFormBase extends \Drupal\Core\Form\FormBase implements \Drupal\Core\Form\ConfirmFormInterface
- class \Drupal\system\Form\PrepareModulesEntityUninstallForm extends \Drupal\Core\Form\ConfirmFormBase
- class \Drupal\Core\Form\ConfirmFormBase extends \Drupal\Core\Form\FormBase implements \Drupal\Core\Form\ConfirmFormInterface
Expanded class hierarchy of PrepareModulesEntityUninstallForm
1 string reference to 'PrepareModulesEntityUninstallForm'
- system.routing.yml in core/
modules/ system/ system.routing.yml - core/modules/system/system.routing.yml
File
-
core/
modules/ system/ src/ Form/ PrepareModulesEntityUninstallForm.php, line 22
Namespace
Drupal\system\FormView source
class PrepareModulesEntityUninstallForm extends ConfirmFormBase {
/**
* The entity type ID of the entities to delete.
*
* @var string
*/
protected $entityTypeId;
/**
* The entity type manager.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
protected $entityTypeManager;
/**
* Constructs a PrepareModulesEntityUninstallForm object.
*
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
* The entity type manager.
*/
public function __construct(EntityTypeManagerInterface $entity_type_manager) {
$this->entityTypeManager = $entity_type_manager;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static($container->get('entity_type.manager'));
}
/**
* {@inheritdoc}
*/
public function getFormId() {
return 'system_prepare_modules_entity_uninstall';
}
/**
* {@inheritdoc}
*/
public function getQuestion() {
$entity_type = $this->entityTypeManager
->getDefinition($this->entityTypeId);
return $this->t('Are you sure you want to delete all @entity_type_plural?', [
'@entity_type_plural' => $entity_type->getPluralLabel(),
]);
}
/**
* {@inheritdoc}
*/
public function getDescription() {
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.');
}
/**
* {@inheritdoc}
*/
public function getConfirmText() {
$entity_type = $this->entityTypeManager
->getDefinition($this->entityTypeId);
return $this->t('Delete all @entity_type_plural', [
'@entity_type_plural' => $entity_type->getPluralLabel(),
]);
}
/**
* {@inheritdoc}
*/
public function getCancelUrl() {
return Url::fromRoute('system.modules_uninstall');
}
/**
* Gets the form title.
*
* @param string $entity_type_id
* The entity type ID.
*
* @return \Drupal\Core\StringTranslation\TranslatableMarkup
* The form title.
*
* @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
* Thrown when the entity-type does not exist.
*/
public function formTitle(string $entity_type_id) : TranslatableMarkup {
$this->entityTypeId = $entity_type_id;
return $this->getQuestion();
}
/**
* Checks access based on the validity of the entity type ID.
*
* @param string $entity_type_id
* Entity type ID.
*
* @return \Drupal\Core\Access\AccessResultInterface
* The access result.
*/
public static function checkAccess(string $entity_type_id) : AccessResultInterface {
return AccessResult::allowedIf(\Drupal::entityTypeManager()->hasDefinition($entity_type_id));
}
/**
* {@inheritdoc}
*/
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;
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
$entity_type_id = $form_state->getValue('entity_type_id');
$entity_type_plural = $this->entityTypeManager
->getDefinition($entity_type_id)
->getPluralLabel();
$batch_builder = (new BatchBuilder())->setTitle($this->t('Deleting @entity_type_plural', [
'@entity_type_plural' => $entity_type_plural,
]))
->setProgressMessage('')
->setFinishCallback([
__CLASS__,
'moduleBatchFinished',
])
->addOperation([
__CLASS__,
'deleteContentEntities',
], [
$entity_type_id,
]);
batch_set($batch_builder->toArray());
}
/**
* Deletes the content entities of the specified entity type.
*
* @param string $entity_type_id
* The entity type ID from which data will be deleted.
* @param array|\ArrayAccess $context
* The batch context array, passed by reference.
*
* @internal
* This batch callback is only meant to be used by this form.
*/
public static function deleteContentEntities($entity_type_id, &$context) {
$storage = \Drupal::entityTypeManager()->getStorage($entity_type_id);
// Set the entity type ID in the results array so we can access it in the
// batch finished callback.
$context['results']['entity_type_id'] = $entity_type_id;
if (!isset($context['sandbox']['progress'])) {
$context['sandbox']['progress'] = 0;
$context['sandbox']['max'] = $storage->getQuery()
->accessCheck(FALSE)
->count()
->execute();
}
$entity_type = \Drupal::entityTypeManager()->getDefinition($entity_type_id);
$entity_ids = $storage->getQuery()
->accessCheck(FALSE)
->sort($entity_type->getKey('id'), 'ASC')
->range(0, 10)
->execute();
if ($entities = $storage->loadMultiple($entity_ids)) {
$storage->delete($entities);
}
// Sometimes deletes cause secondary deletes. For example, deleting a
// taxonomy term can cause its children to be deleted too.
$context['sandbox']['progress'] = $context['sandbox']['max'] - $storage->getQuery()
->accessCheck(FALSE)
->count()
->execute();
// Inform the batch engine that we are not finished and provide an
// estimation of the completion level we reached.
if (count($entity_ids) > 0 && $context['sandbox']['progress'] != $context['sandbox']['max']) {
$context['finished'] = $context['sandbox']['progress'] / $context['sandbox']['max'];
$context['message'] = new TranslatableMarkup('Deleting items... Completed @percentage% (@current of @total).', [
'@percentage' => round(100 * $context['sandbox']['progress'] / $context['sandbox']['max']),
'@current' => $context['sandbox']['progress'],
'@total' => $context['sandbox']['max'],
]);
}
else {
$context['finished'] = 1;
}
}
/**
* Implements callback_batch_finished().
*
* Finishes the module batch, redirect to the uninstall page and output the
* successful data deletion message.
*/
public static function moduleBatchFinished($success, $results, $operations) {
$entity_type_plural = \Drupal::entityTypeManager()->getDefinition($results['entity_type_id'])
->getPluralLabel();
\Drupal::messenger()->addStatus(new TranslatableMarkup('All @entity_type_plural have been deleted.', [
'@entity_type_plural' => $entity_type_plural,
]));
return new RedirectResponse(Url::fromRoute('system.modules_uninstall')->setAbsolute()
->toString());
}
}
Members
Title Sort descending | Modifiers | Object type | Summary | Overriden Title | Overrides |
---|---|---|---|---|---|
ConfirmFormBase::getCancelText | public | function | Returns a caption for the link which cancels the action. | Overrides ConfirmFormInterface::getCancelText | 2 |
ConfirmFormBase::getFormName | public | function | Returns the internal name used to refer to the confirmation item. | Overrides ConfirmFormInterface::getFormName | |
DependencySerializationTrait::$_entityStorages | protected | property | |||
DependencySerializationTrait::$_serviceIds | protected | property | |||
DependencySerializationTrait::__sleep | public | function | 1 | ||
DependencySerializationTrait::__wakeup | public | function | 2 | ||
FormBase::$configFactory | protected | property | The config factory. | 3 | |
FormBase::$requestStack | protected | property | The request stack. | 1 | |
FormBase::$routeMatch | protected | property | The route match. | ||
FormBase::config | protected | function | Retrieves a configuration object. | ||
FormBase::configFactory | protected | function | Gets the config factory for this form. | 3 | |
FormBase::container | private | function | Returns the service container. | ||
FormBase::currentUser | protected | function | Gets the current user. | 2 | |
FormBase::getRequest | protected | function | Gets the request object. | ||
FormBase::getRouteMatch | protected | function | Gets the route match. | ||
FormBase::logger | protected | function | Gets the logger for a specific channel. | ||
FormBase::redirect | protected | function | Returns a redirect response object for the specified route. | ||
FormBase::resetConfigFactory | public | function | Resets the configuration factory. | ||
FormBase::setConfigFactory | public | function | Sets the config factory for this form. | ||
FormBase::setRequestStack | public | function | Sets the request stack object to use. | ||
FormBase::validateForm | public | function | Form validation handler. | Overrides FormInterface::validateForm | 57 |
LoggerChannelTrait::$loggerFactory | protected | property | The logger channel factory service. | ||
LoggerChannelTrait::getLogger | protected | function | Gets the logger for a specific channel. | ||
LoggerChannelTrait::setLoggerFactory | public | function | Injects the logger channel factory. | ||
MessengerTrait::$messenger | protected | property | The messenger. | 16 | |
MessengerTrait::messenger | public | function | Gets the messenger. | 16 | |
MessengerTrait::setMessenger | public | function | Sets the messenger. | ||
PrepareModulesEntityUninstallForm::$entityTypeId | protected | property | The entity type ID of the entities to delete. | ||
PrepareModulesEntityUninstallForm::$entityTypeManager | protected | property | The entity type manager. | ||
PrepareModulesEntityUninstallForm::buildForm | public | function | Form constructor. | Overrides ConfirmFormBase::buildForm | |
PrepareModulesEntityUninstallForm::checkAccess | public static | function | Checks access based on the validity of the entity type ID. | ||
PrepareModulesEntityUninstallForm::create | public static | function | Instantiates a new instance of this class. | Overrides FormBase::create | |
PrepareModulesEntityUninstallForm::deleteContentEntities | public static | function | Deletes the content entities of the specified entity type. | ||
PrepareModulesEntityUninstallForm::formTitle | public | function | Gets the form title. | ||
PrepareModulesEntityUninstallForm::getCancelUrl | public | function | Returns the route to go to if the user cancels the action. | Overrides ConfirmFormInterface::getCancelUrl | |
PrepareModulesEntityUninstallForm::getConfirmText | public | function | Returns a caption for the button that confirms the action. | Overrides ConfirmFormBase::getConfirmText | |
PrepareModulesEntityUninstallForm::getDescription | public | function | Returns additional text to display as a description. | Overrides ConfirmFormBase::getDescription | |
PrepareModulesEntityUninstallForm::getFormId | public | function | Returns a unique string identifying the form. | Overrides FormInterface::getFormId | |
PrepareModulesEntityUninstallForm::getQuestion | public | function | Returns the question to ask the user. | Overrides ConfirmFormInterface::getQuestion | |
PrepareModulesEntityUninstallForm::moduleBatchFinished | public static | function | Implements callback_batch_finished(). | ||
PrepareModulesEntityUninstallForm::submitForm | public | function | Form submission handler. | Overrides FormInterface::submitForm | |
PrepareModulesEntityUninstallForm::__construct | public | function | Constructs a PrepareModulesEntityUninstallForm object. | ||
RedirectDestinationTrait::$redirectDestination | protected | property | The redirect destination service. | 2 | |
RedirectDestinationTrait::getDestinationArray | protected | function | Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url. | ||
RedirectDestinationTrait::getRedirectDestination | protected | function | Returns the redirect destination service. | ||
RedirectDestinationTrait::setRedirectDestination | public | function | Sets the redirect destination service. | ||
StringTranslationTrait::$stringTranslation | protected | property | The string translation service. | 3 | |
StringTranslationTrait::formatPlural | protected | function | Formats a string containing a count of items. | ||
StringTranslationTrait::getNumberOfPlurals | protected | function | Returns the number of plurals supported by a given language. | ||
StringTranslationTrait::getStringTranslation | protected | function | Gets the string translation service. | ||
StringTranslationTrait::setStringTranslation | public | function | Sets the string translation service to use. | 2 | |
StringTranslationTrait::t | protected | function | Translates a string to the current language or to a given language. |
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.