Feeds Tamper does this job, which allows me to "tamper" with feed items' values before saving it to an entity.

My question is how can I do that programmatically in Drupal 8 with a custom module.


Thanks to No Sssweat, I managed to use an EventSubscriber using afterParse event to do this job.

File 1: mycustommodule/mycustommodule.services.yml

   class: Drupal\mycustommodule\EventSubscriber\FeedsSubscriber
     - {name: event_subscriber}

File 2: mycustommodule/src/EventSubscriber/FeedsSubscriber.php


namespace Drupal\mycustommodule\EventSubscriber;

use Drupal\feeds\Event\FeedsEvents;
use Drupal\feeds\Event\ParseEvent;
use Drupal\feeds\Feeds\Item\ItemInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

 * Subscriber to Feeds events.
 * This happens after parsing and before going
 * into processing.
class FeedsSubscriber implements EventSubscriberInterface {

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

   * Acts on parser result.
  public function afterParse(ParseEvent $event) {

    /** @var \Drupal\feeds\FeedInterface $feed */
    $feed = $event->getFeed();
    $feed_bundle_name = $feed->bundle();

    // Only alter a particular feed
    if ($feed_bundle_name == "MY_FEED_IMPORTER_MACHINE_NAME") {

      /** @var \Drupal\feeds\Result\ParserResultInterface $result */
      $result = $event->getParserResult();

      for ($i = 0; $i < $result->count(); $i++) {
        if (!$result->offsetExists($i)) {

        /** @var \Drupal\feeds\Feeds\Item\ItemInterface $item */
        $item = $result->offsetGet($i);

         * My custom code here using $item->get() and $item->set()
         * to alter whatever values I want.
  • Is there a way to check a value of the source field before Tamper changes it i.e. with a Find and Replace? I am trying to Skip items using SkipItemException found in the latest Feeds releases and even in alterItem the source has already been changed by Tamper. I need to keep the Tamper settings in order to match categories for targeting my terms but need to skip rows based on source.
    – Shardon
    May 13 '20 at 8:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.