You are here

public function SpambotUserspamForm::entitiesUnpublish in Spambot 8

Processor for unpublish batch operations.

Parameters

array|null $items: Array of entities id.

string $type: Type of entity.

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 721

Class

SpambotUserspamForm
Settings form to save the configuration for Spambot.

Namespace

Drupal\spambot\Form

Code

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

  // Elements per operation.
  $limit = 50;
  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) {
        $entity = $this->entityTypeManager
          ->getStorage($type)
          ->load($item);
        $entity
          ->setPublished(FALSE);
        $entity
          ->save();
        $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'];
  }
}