You are here

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

Expanded class hierarchy of PrepareHttpblEntityUninstallForm

1 string reference to 'PrepareHttpblEntityUninstallForm'
httpbl.routing.yml in ./httpbl.routing.yml
httpbl.routing.yml

File

src/Form/PrepareHttpblEntityUninstallForm.php, line 22

Namespace

Drupal\httpbl\Form
View 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

Namesort descending Modifiers Type Description Overrides
ConfirmFormBase::getCancelText public function Returns a caption for the link which cancels the action. Overrides ConfirmFormInterface::getCancelText 1
ConfirmFormBase::getFormName public function Returns the internal name used to refer to the confirmation item. Overrides ConfirmFormInterface::getFormName
DependencySerializationTrait::$_entityStorages protected property An array of entity type IDs keyed by the property name of their storages.
DependencySerializationTrait::$_serviceIds protected property An array of service IDs keyed by property name used for serialization.
DependencySerializationTrait::__sleep public function 1
DependencySerializationTrait::__wakeup public function 2
FormBase::$configFactory protected property The config factory. 1
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. 1
FormBase::container private function Returns the service container.
FormBase::currentUser protected function Gets the current user.
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. Overrides UrlGeneratorTrait::redirect
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 62
LinkGeneratorTrait::$linkGenerator protected property The link generator. 1
LinkGeneratorTrait::getLinkGenerator Deprecated protected function Returns the link generator.
LinkGeneratorTrait::l Deprecated protected function Renders a link to a route given a route name and its parameters.
LinkGeneratorTrait::setLinkGenerator Deprecated public function Sets the link generator service.
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. 29
MessengerTrait::messenger public function Gets the messenger. 29
MessengerTrait::setMessenger public function Sets the messenger.
PrepareHttpblEntityUninstallForm::$entityTypeId protected property The entity type ID of the entities to delete. Overrides PrepareModulesEntityUninstallForm::$entityTypeId
PrepareHttpblEntityUninstallForm::$entityTypeManager protected property The entity type manager. Overrides PrepareModulesEntityUninstallForm::$entityTypeManager
PrepareHttpblEntityUninstallForm::create public static function Instantiates a new instance of this class. Overrides PrepareModulesEntityUninstallForm::create
PrepareHttpblEntityUninstallForm::deleteContentEntities public static function Deletes the content entities of the specified entity type. Overrides PrepareModulesEntityUninstallForm::deleteContentEntities
PrepareHttpblEntityUninstallForm::getConfirmText public function Returns a caption for the button that confirms the action. Overrides PrepareModulesEntityUninstallForm::getConfirmText
PrepareHttpblEntityUninstallForm::getDescription public function Returns additional text to display as a description. Overrides PrepareModulesEntityUninstallForm::getDescription
PrepareHttpblEntityUninstallForm::getFormId public function Returns a unique string identifying the form. Overrides PrepareModulesEntityUninstallForm::getFormId
PrepareHttpblEntityUninstallForm::getQuestion public function Returns the question to ask the user. Overrides PrepareModulesEntityUninstallForm::getQuestion
PrepareHttpblEntityUninstallForm::submitForm public function Form submission handler. Overrides PrepareModulesEntityUninstallForm::submitForm
PrepareHttpblEntityUninstallForm::__construct public function Constructs a PrepareModulesEntityUninstallForm object. Overrides PrepareModulesEntityUninstallForm::__construct
PrepareModulesEntityUninstallForm::buildForm public function Form constructor. Overrides ConfirmFormBase::buildForm
PrepareModulesEntityUninstallForm::getCancelUrl public function Returns the route to go to if the user cancels the action. Overrides ConfirmFormInterface::getCancelUrl
PrepareModulesEntityUninstallForm::moduleBatchFinished public static function Implements callback_batch_finished().
RedirectDestinationTrait::$redirectDestination protected property The redirect destination service. 1
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. 1
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.
UrlGeneratorTrait::$urlGenerator protected property The url generator.
UrlGeneratorTrait::getUrlGenerator Deprecated protected function Returns the URL generator service.
UrlGeneratorTrait::setUrlGenerator Deprecated public function Sets the URL generator service.
UrlGeneratorTrait::url Deprecated protected function Generates a URL or path for a specific route based on the given parameters.