You are here

public function EntityProcessorBase::clean in Feeds 8.3

Applies an action to an entity to 'clean' it.

An action is applied on an entity for which the source item no longer exists in the feed.

An action can be:

  • Deleting the entity;
  • Unpublishing the entity;
  • Or any other action plugin that is applyable.

Parameters

\Drupal\feeds\FeedInterface $feed: The feed entity.

\Drupal\Core\Entity\EntityInterface $entity: The entity being cleaned.

\Drupal\feeds\Feeds\State\CleanStateInterface $state: The state object.

Overrides CleanableInterface::clean

File

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

Class

EntityProcessorBase
Defines a base entity processor.

Namespace

Drupal\feeds\Feeds\Processor

Code

public function clean(FeedInterface $feed, EntityInterface $entity, CleanStateInterface $state) {
  $update_non_existent = $this
    ->getConfiguration('update_non_existent');
  if ($update_non_existent === static::KEEP_NON_EXISTENT) {

    // No action to take on this entity.
    return;
  }
  switch ($update_non_existent) {
    case static::KEEP_NON_EXISTENT:

      // No action to take on this entity.
      return;
    case static::DELETE_NON_EXISTENT:
      $entity
        ->delete();
      break;
    default:
      try {

        // Apply action on entity.
        \Drupal::service('plugin.manager.action')
          ->createInstance($update_non_existent)
          ->execute($entity);
      } catch (PluginNotFoundException $e) {
        $state
          ->setMessage(t('Cleaning %entity failed because of non-existing action plugin %name.', [
          '%entity' => $entity
            ->label(),
          '%name' => $update_non_existent,
        ]), 'error');
        throw $e;
      }
      break;
  }

  // Check if the entity was deleted.
  $entity = $this->storageController
    ->load($entity
    ->id());

  // If the entity was not deleted, update hash.
  if (isset($entity->feeds_item)) {
    $entity
      ->get('feeds_item')->hash = $update_non_existent;
    $this->storageController
      ->save($entity);
  }

  // State progress.
  $state->updated++;
  $state
    ->progress($state->total, $state->updated);
}