You are here

class FeedClearHandler in Feeds 8.3

Deletes the items of a feed.

Hierarchy

Expanded class hierarchy of FeedClearHandler

1 file declares its use of FeedClearHandler
FeedClearHandlerTest.php in tests/src/Unit/FeedClearHandlerTest.php

File

src/FeedClearHandler.php, line 12

Namespace

Drupal\feeds
View source
class FeedClearHandler extends FeedHandlerBase {

  /**
   * {@inheritdoc}
   */
  public function startBatchClear(FeedInterface $feed) {
    $feed
      ->lock();
    $feed
      ->clearStates();
    $batch = [
      'title' => $this
        ->t('Deleting items from: %title', [
        '%title' => $feed
          ->label(),
      ]),
      'init_message' => $this
        ->t('Deleting items from: %title', [
        '%title' => $feed
          ->label(),
      ]),
      'operations' => [
        [
          [
            $this,
            'clear',
          ],
          [
            $feed,
          ],
        ],
      ],
      'progress_message' => $this
        ->t('Deleting items from: %title', [
        '%title' => $feed
          ->label(),
      ]),
      'error_message' => $this
        ->t('An error occored while clearing %title.', [
        '%title' => $feed
          ->label(),
      ]),
    ];
    $this
      ->batchSet($batch);
  }

  /**
   * Deletes all items from a feed.
   *
   * @param \Drupal\feeds\FeedInterface $feed
   *   The feed entity from which to delete all items.
   * @param array $context
   *   Status of the batch.
   */
  public function clear(FeedInterface $feed, array &$context) {
    try {
      $this
        ->dispatchEvent(FeedsEvents::INIT_CLEAR, new InitEvent($feed));
      $this
        ->dispatchEvent(FeedsEvents::CLEAR, new ClearEvent($feed));
    } catch (\Exception $exception) {

      // Do nothing yet.
    }

    // Clean up.
    $context['finished'] = $feed
      ->progressClearing();
    if (isset($exception)) {
      $context['finished'] = StateInterface::BATCH_COMPLETE;
    }
    if ($context['finished'] === StateInterface::BATCH_COMPLETE) {
      $feed
        ->finishClear();
      $feed
        ->save();
      $feed
        ->unlock();
    }
    else {
      $feed
        ->saveStates();
    }
    if (isset($exception)) {
      throw $exception;
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DependencySerializationTrait::$_entityStorages protected property An array of entity type IDs keyed by the property name of their storages.
DependencySerializationTrait::$_serviceIds protected property An array of service IDs keyed by property name used for serialization.
DependencySerializationTrait::__sleep public function 1
DependencySerializationTrait::__wakeup public function 2
EventDispatcherTrait::$_eventDispatcher private property The event dispatcher service.
EventDispatcherTrait::dispatchEvent protected function Dispatches an event.
EventDispatcherTrait::getEventDispatcher protected function Returns the event dispatcher service.
EventDispatcherTrait::setEventDispatcher public function Sets the event dispatcher service to use.
FeedClearHandler::clear public function Deletes all items from a feed.
FeedClearHandler::startBatchClear public function
FeedHandlerBase::batchSet protected function Adds a new batch.
FeedHandlerBase::createInstance public static function Instantiates a new instance of this entity handler. Overrides EntityHandlerInterface::createInstance
FeedHandlerBase::__construct public function Constructs a new FeedHandlerBase object.
MessengerTrait::$messenger protected property The messenger. 29
MessengerTrait::messenger public function Gets the messenger. 29
MessengerTrait::setMessenger public function Sets the messenger.
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.