You are here

public static function PrepareHttpblEntityUninstallForm::deleteContentEntities in http:BL 8

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.

@internal This batch callback is only meant to be used by this form.

Parameters

string $entity_type_id: The entity type ID from which data will be deleted.

array|\ArrayAccess $context: The batch context array, passed by reference.

Overrides PrepareModulesEntityUninstallForm::deleteContentEntities

See also

comments below for details on what is being overridden.

File

src/Form/PrepareHttpblEntityUninstallForm.php, line 136

Class

PrepareHttpblEntityUninstallForm
Provides a form removing httpbl content entities data before uninstallation.

Namespace

Drupal\httpbl\Form

Code

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;
  }
}