You are here

public function SpambotUserspamForm::deleteEntitiesBatch in Spambot 8

Processor for delete batch operations.

Parameters

array|null $items: Array of entities id.

string $type: Entity type.

array $context: Batch data.

Throws

\Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException

\Drupal\Component\Plugin\Exception\PluginNotFoundException

\Drupal\Core\Entity\EntityStorageException

File

src/Form/SpambotUserspamForm.php, line 629

Class

SpambotUserspamForm
Settings form to save the configuration for Spambot.

Namespace

Drupal\spambot\Form

Code

public function deleteEntitiesBatch($items, $type, array &$context) {

  // Elements per operation.
  $limit = 50;

  // Set default progress values.
  if (empty($context['sandbox']['progress'])) {
    $context['sandbox']['progress'] = 0;
    $context['sandbox']['max'] = count($items);
  }
  if (empty($context['sandbox']['items'])) {
    $context['sandbox']['items'] = $items;
  }
  $counter = 0;
  if (!empty($context['sandbox']['items'])) {

    // Remove already processed items.
    if ($context['sandbox']['progress'] != 0) {
      array_splice($context['sandbox']['items'], 0, $limit);
    }
    foreach ($context['sandbox']['items'] as $item) {
      if ($counter !== $limit) {
        static::deleteEntity($item, $type);
        $counter++;
        $context['sandbox']['progress']++;
        $context['message'] = $this
          ->t('Now processing :entity :progress of :count', [
          ':entity' => $type,
          ':progress' => $context['sandbox']['progress'],
          ':count' => $context['sandbox']['max'],
        ]);
        $context['results']['processed'] = $context['sandbox']['progress'];
      }
    }
  }
  if ($context['sandbox']['progress'] != $context['sandbox']['max']) {
    $context['finished'] = $context['sandbox']['progress'] / $context['sandbox']['max'];
  }
}