You are here

public function EntityProcessorBase::clear in Feeds 8.3

Removes all stored results for a feed.

This can be implemented by any plugin type and the method will be called when a feed is being cleared (having its items deleted.) This is useful if the plugin caches or stores information related to a feed.

This operation supports batching in the same way that importing does. You can get the state object from the feed.

$state = $feed
  ->getState(StateInterface::CLEAR);
$state->total = (int) find_total($feed
  ->id());
$state
  ->progress($state->total, $state->total - $deleted);

Parameters

\Drupal\feeds\FeedInterface $feed: The feed being cleared. Implementers should only delete items pertaining to this feed. The preferred way of determining whether an item pertains to a certain feed is by using $feed->id(). It is the plugins's responsibility to store the id of an imported item during importing.

\Drupal\feeds\StateInterface $state: The state object.

Overrides ClearableInterface::clear

File

src/Feeds/Processor/EntityProcessorBase.php, line 303

Class

EntityProcessorBase
Defines a base entity processor.

Namespace

Drupal\feeds\Feeds\Processor

Code

public function clear(FeedInterface $feed, StateInterface $state) {

  // Build base select statement.
  $query = $this->entityTypeManager
    ->getStorage($this
    ->entityType())
    ->getQuery()
    ->condition('feeds_item.target_id', $feed
    ->id());

  // If there is no total, query it.
  if (!$state->total) {
    $count_query = clone $query;
    $state->total = (int) $count_query
      ->count()
      ->execute();
  }

  // Delete a batch of entities.
  $entity_ids = $query
    ->range(0, 10)
    ->execute();
  if ($entity_ids) {
    $this
      ->entityDeleteMultiple($entity_ids);
    $state->deleted += count($entity_ids);
    $state
      ->progress($state->total, $state->deleted);
  }
}