class PrepareHttpblEntityUninstallForm in http:BL 8
Provides a form removing httpbl content entities data before uninstallation.
Important! This overrides the core method of removing module entities because we also need to cleanup any records in the Ban module's table that were put there by Httpbl.
Hierarchy
- class \Drupal\Core\Form\FormBase implements ContainerInjectionInterface, FormInterface uses DependencySerializationTrait, LoggerChannelTrait, MessengerTrait, LinkGeneratorTrait, RedirectDestinationTrait, UrlGeneratorTrait, StringTranslationTrait
- class \Drupal\Core\Form\ConfirmFormBase implements ConfirmFormInterface
- class \Drupal\system\Form\PrepareModulesEntityUninstallForm
- class \Drupal\httpbl\Form\PrepareHttpblEntityUninstallForm
- class \Drupal\system\Form\PrepareModulesEntityUninstallForm
- class \Drupal\Core\Form\ConfirmFormBase implements ConfirmFormInterface
Expanded class hierarchy of PrepareHttpblEntityUninstallForm
1 string reference to 'PrepareHttpblEntityUninstallForm'
File
- src/
Form/ PrepareHttpblEntityUninstallForm.php, line 22
Namespace
Drupal\httpbl\FormView source
class PrepareHttpblEntityUninstallForm extends PrepareModulesEntityUninstallForm {
/**
* 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) {
parent::__construct($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 'httpbl_prepare_modules_entity_uninstall';
}
/**
* {@inheritdoc}
*/
public function getQuestion() {
$entity_type = $this->entityTypeManager
->getDefinition($this->entityTypeId);
return $this
->t('You are about to delete all @entity_type_plural + Ban_ip records banned by HttpBL. Are you sure you want to do this?', [
'@entity_type_plural' => $entity_type
->getPluralLabel(),
]);
}
/**
* {@inheritdoc}
*/
public function getDescription() {
$check_link = Link::fromTextAndUrl(t('Please ensure all Http:BL Blocking is disabled'), Url::fromRoute('httpbl.admin_config'))
->toString();
$blacklist_report_link = Link::fromTextAndUrl(t('Http:BL blacklisted hosts'), Url::fromRoute('view.evaluated_hosts.page_banned'))
->toString();
// @see for the clue to this route. http://drupal.stackexchange.com/questions/223405/how-to-get-route-name-of-a-view-page
$message = $this
->t('@check (otherwise new entities will be created during the process of deleting them).<br />', [
'@check' => $check_link,
]);
$message .= $this
->t('This action affects two tables <strong>(httpbl_host and ban_ip)</strong> and cannot be undone.<br />');
$message .= $this
->t('Any blacklisted hosts in Http:BL will also be removed from Ban if found there.<br />');
$message .= $this
->t('You can preview all @blacklisted here. These will be un-banned.<br />', [
'@blacklisted' => $blacklist_report_link,
]);
$message .= $this
->t('Make a backup of your database if you want to be able to restore these items.');
return $message;
}
/**
* {@inheritdoc}
*/
public function getConfirmText() {
$entity_type = $this->entityTypeManager
->getDefinition($this->entityTypeId);
return $this
->t('Delete and Un-Ban all @entity_type_plural', [
'@entity_type_plural' => $entity_type
->getPluralLabel(),
]);
}
/**
* {@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 = [
'title' => t('Deleting @entity_type_plural', [
'@entity_type_plural' => $entity_type_plural,
]),
'operations' => [
[
[
__CLASS__,
'deleteContentEntities',
],
[
$entity_type_id,
],
],
],
'finished' => [
__CLASS__,
'moduleBatchFinished',
],
'progress_message' => '',
];
batch_set($batch);
}
/**
* Deletes the content entities of the specified entity type.
*
* This function overrides Drupal core, in order to also manage non-entity
* records in Ban module, created by this module.
* @see comments below for details on what is being overridden.
*
* @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()
->count()
->execute();
}
$entity_type = \Drupal::entityTypeManager()
->getDefinition($entity_type_id);
$entity_ids = $storage
->getQuery()
->sort($entity_type
->getKey('id'), 'ASC')
->range(0, 10)
->execute();
if ($entities = $storage
->loadMultiple($entity_ids)) {
//-----------------------------------------------------------------------
// HERE'S THE OVERRIDE (everything in this function up to this point is core)!
// Before deleting a batch of host entities, use them to find any matching
// IPs in Ban module.
//
// Call BanIpManager service and check if this Host is also banned.
$banManager = \Drupal::service('ban.ip_manager');
foreach ($entities as $key => $host) {
$host = Host::load($key);
$host_ip = $host
->getHostIp();
// Find IPs that have also been banned by Httpbl.
$banned = $banManager
->isBanned($host_ip);
// If banned (by Httpbl), un-ban them.
if ($banned) {
$banManager
->unBanIp($host_ip);
$message = new FormattableMarkup('Unbanned @ip banned by Httpbl while uninstalled.', [
'@ip' => $host_ip,
]);
\Drupal::logger('httpbl')
->warning($message);
}
}
// END OF OVERRIDE. Now remove the entities.
// ----------------------------------------------------------------------
$storage
->delete($entities);
}
// Sometimes deletes cause secondary deletes. For example, deleting a
// taxonomy term can cause it's children to be be deleted too.
$context['sandbox']['progress'] = $context['sandbox']['max'] - $storage
->getQuery()
->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'] = 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'],
]);
}
else {
$context['finished'] = 1;
}
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
ConfirmFormBase:: |
public | function |
Returns a caption for the link which cancels the action. Overrides ConfirmFormInterface:: |
1 |
ConfirmFormBase:: |
public | function |
Returns the internal name used to refer to the confirmation item. Overrides ConfirmFormInterface:: |
|
DependencySerializationTrait:: |
protected | property | An array of entity type IDs keyed by the property name of their storages. | |
DependencySerializationTrait:: |
protected | property | An array of service IDs keyed by property name used for serialization. | |
DependencySerializationTrait:: |
public | function | 1 | |
DependencySerializationTrait:: |
public | function | 2 | |
FormBase:: |
protected | property | The config factory. | 1 |
FormBase:: |
protected | property | The request stack. | 1 |
FormBase:: |
protected | property | The route match. | |
FormBase:: |
protected | function | Retrieves a configuration object. | |
FormBase:: |
protected | function | Gets the config factory for this form. | 1 |
FormBase:: |
private | function | Returns the service container. | |
FormBase:: |
protected | function | Gets the current user. | |
FormBase:: |
protected | function | Gets the request object. | |
FormBase:: |
protected | function | Gets the route match. | |
FormBase:: |
protected | function | Gets the logger for a specific channel. | |
FormBase:: |
protected | function |
Returns a redirect response object for the specified route. Overrides UrlGeneratorTrait:: |
|
FormBase:: |
public | function | Resets the configuration factory. | |
FormBase:: |
public | function | Sets the config factory for this form. | |
FormBase:: |
public | function | Sets the request stack object to use. | |
FormBase:: |
public | function |
Form validation handler. Overrides FormInterface:: |
62 |
LinkGeneratorTrait:: |
protected | property | The link generator. | 1 |
LinkGeneratorTrait:: |
protected | function | Returns the link generator. | |
LinkGeneratorTrait:: |
protected | function | Renders a link to a route given a route name and its parameters. | |
LinkGeneratorTrait:: |
public | function | Sets the link generator service. | |
LoggerChannelTrait:: |
protected | property | The logger channel factory service. | |
LoggerChannelTrait:: |
protected | function | Gets the logger for a specific channel. | |
LoggerChannelTrait:: |
public | function | Injects the logger channel factory. | |
MessengerTrait:: |
protected | property | The messenger. | 29 |
MessengerTrait:: |
public | function | Gets the messenger. | 29 |
MessengerTrait:: |
public | function | Sets the messenger. | |
PrepareHttpblEntityUninstallForm:: |
protected | property |
The entity type ID of the entities to delete. Overrides PrepareModulesEntityUninstallForm:: |
|
PrepareHttpblEntityUninstallForm:: |
protected | property |
The entity type manager. Overrides PrepareModulesEntityUninstallForm:: |
|
PrepareHttpblEntityUninstallForm:: |
public static | function |
Instantiates a new instance of this class. Overrides PrepareModulesEntityUninstallForm:: |
|
PrepareHttpblEntityUninstallForm:: |
public static | function |
Deletes the content entities of the specified entity type. Overrides PrepareModulesEntityUninstallForm:: |
|
PrepareHttpblEntityUninstallForm:: |
public | function |
Returns a caption for the button that confirms the action. Overrides PrepareModulesEntityUninstallForm:: |
|
PrepareHttpblEntityUninstallForm:: |
public | function |
Returns additional text to display as a description. Overrides PrepareModulesEntityUninstallForm:: |
|
PrepareHttpblEntityUninstallForm:: |
public | function |
Returns a unique string identifying the form. Overrides PrepareModulesEntityUninstallForm:: |
|
PrepareHttpblEntityUninstallForm:: |
public | function |
Returns the question to ask the user. Overrides PrepareModulesEntityUninstallForm:: |
|
PrepareHttpblEntityUninstallForm:: |
public | function |
Form submission handler. Overrides PrepareModulesEntityUninstallForm:: |
|
PrepareHttpblEntityUninstallForm:: |
public | function |
Constructs a PrepareModulesEntityUninstallForm object. Overrides PrepareModulesEntityUninstallForm:: |
|
PrepareModulesEntityUninstallForm:: |
public | function |
Form constructor. Overrides ConfirmFormBase:: |
|
PrepareModulesEntityUninstallForm:: |
public | function |
Returns the route to go to if the user cancels the action. Overrides ConfirmFormInterface:: |
|
PrepareModulesEntityUninstallForm:: |
public static | function | Implements callback_batch_finished(). | |
RedirectDestinationTrait:: |
protected | property | The redirect destination service. | 1 |
RedirectDestinationTrait:: |
protected | function | Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url. | |
RedirectDestinationTrait:: |
protected | function | Returns the redirect destination service. | |
RedirectDestinationTrait:: |
public | function | Sets the redirect destination service. | |
StringTranslationTrait:: |
protected | property | The string translation service. | 1 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. | |
UrlGeneratorTrait:: |
protected | property | The url generator. | |
UrlGeneratorTrait:: |
protected | function | Returns the URL generator service. | |
UrlGeneratorTrait:: |
public | function | Sets the URL generator service. | |
UrlGeneratorTrait:: |
protected | function | Generates a URL or path for a specific route based on the given parameters. |