You are here

abstract class AfterParseBase in Feeds 8.3

A base class for manipulating parser results.

Hierarchy

  • class \Drupal\feeds\EventSubscriber\AfterParseBase implements \Symfony\Component\EventDispatcher\EventSubscriberInterface

Expanded class hierarchy of AfterParseBase

1 file declares its use of AfterParseBase
AfterParseBaseTest.php in tests/src/Unit/EventSubscriber/AfterParseBaseTest.php

File

src/EventSubscriber/AfterParseBase.php, line 14

Namespace

Drupal\feeds\EventSubscriber
View source
abstract class AfterParseBase implements EventSubscriberInterface {

  /**
   * {@inheritdoc}
   */
  public static function getSubscribedEvents() {
    $events = [];
    $events[FeedsEvents::PARSE][] = [
      'afterParse',
      FeedsEvents::AFTER,
    ];
    return $events;
  }

  /**
   * Acts on parser result.
   *
   * @param \Drupal\feeds\Event\ParseEvent $event
   *   The parse event.
   */
  public function afterParse(ParseEvent $event) {
    if (!$this
      ->applies($event)) {
      return;
    }

    /** @var \Drupal\feeds\Result\ParserResultInterface $result */
    $result = $event
      ->getParserResult();
    for ($i = 0; $i < $result
      ->count(); $i++) {
      if (!$result
        ->offsetExists($i)) {
        break;
      }

      /** @var \Drupal\feeds\Feeds\Item\ItemInterface $item */
      $item = $result
        ->offsetGet($i);
      try {
        $this
          ->alterItem($item, $event);
      } catch (SkipItemException $e) {
        $result
          ->offsetUnset($i);
        $i--;
      }
    }
  }

  /**
   * Returns if parsing should apply.
   *
   * @param \Drupal\feeds\Event\ParseEvent $event
   *   The parse event.
   *
   * @return bool
   *   True, if altering should continue.
   *   False otherwise.
   */
  public function applies(ParseEvent $event) {
    return TRUE;
  }

  /**
   * Alters a single item.
   *
   * @param \Drupal\feeds\Feeds\Item\ItemInterface $item
   *   The item to make modifications on.
   * @param \Drupal\feeds\Event\ParseEvent $event
   *   The parse event.
   *
   * @throws \Drupal\feeds\Exception\SkipItemException
   *   In case the item should not be imported.
   */
  protected function alterItem(ItemInterface $item, ParseEvent $event) {
  }

}

Members

Namesort descending Modifiers Type Description Overrides
AfterParseBase::afterParse public function Acts on parser result.
AfterParseBase::alterItem protected function Alters a single item.
AfterParseBase::applies public function Returns if parsing should apply.
AfterParseBase::getSubscribedEvents public static function Returns an array of event names this subscriber wants to listen to.