6

I am wondering how to implement a Hook after the import of one of my importers is finished. The last imported node and the name of the importer itself would be the parameters that i need to have at this time.

Background/Usecase:

The site I'm working on is built to control IT-projects. After the user imports the bill-positions (each one a row in a CSV file => node in Drupal), which happens monthly, the site has to check if all bill-positions for a customer have been invoiced.

This is only one example, there are many other examinations that need to be done after an import is done.

I read the Feeds API & documentation, but I didn't find anything useful.

P.S. I'm pretty new to open source and Drupal itself; so please correct me if I am missing any guideline.

2 Answers 2

1

I have never used this, but you can try hook_feeds_after_import. You will likely need to kpr the $source parameter to the hook.

You may also be able to something with Rules. Feeds events don't appear in Rules (unless there is a module I don't know about), but you may be able to trigger on the node save and update events filtered to the content type(s) that you are importing to.

3
  • The hook you mentioned executes after a single line is imported, not the hole file. Got to check the Rules module, but to trigger on a single node_save or node_update is not what i am looking for. As i said, i need to find a way to hook me in, after the import of a hole file is done.
    – Highclass
    May 30, 2012 at 8:47
  • 1
    I don't know how you make use of a hook after the whole file import though... It's a batch operation so you can use your own custom callback function upon successful submission. Not sure if you can alter it using a module though
    – AKS
    Jul 28, 2012 at 17:50
  • The hook_feeds_after_import is called after the whole file is processed. It can be used for such things.
    – mouhammed
    Sep 11, 2015 at 8:24
0

If you're looking at this post and wondering why your hook is not working is because in the D8 version of Feeds, all hooks are gone and most of them are converted to events.

The equivalent for this hook is the event \Drupal\feeds\Event\FeedsEvents::IMPORT_FINISHED. In the event callback you’ll get an object of type \Drupal\feeds\Event\ImportFinishedEvent.

To listen to this event:

  1. Define your event subscriber in your module’s mymodule.services.yml file:
    services:
      mymodule.feeds_subscriber:
        class: Drupal\mymodule\EventSubscriber\FeedsSubscriber
        tags:
          - { name: event_subscriber }
  1. Create your event subscriber class in mymodule/src/EventSubscriber/FeedsSubscriber.php and subscribe to \Drupal\feeds\Event\FeedsEvents::IMPORT_FINISHED:
    <?php

    namespace Drupal\mymodule\EventSubscriber;

    use Drupal\feeds\Event\FeedsEvents;
    use Drupal\feeds\Event\ImportFinishedEvent;
    use Symfony\Component\EventDispatcher\EventSubscriberInterface;

    /**
     * (describe what your event listener does here)
     */
    class FeedsSubscriber implements EventSubscriberInterface {

      /**
       * {@inheritdoc}
       */
      public static function getSubscribedEvents() {
        $events = [];
        $events[FeedsEvents::IMPORT_FINISHED][] = 'afterImport';
        return $events;
      }

      /**
       * Called when an import has finished.
       *
       * @param \Drupal\feeds\Event\ImportFinishedEvent $event
       *   The import finished event.
       */
      public function afterImport(ImportFinishedEvent $event) {
        // (your code here...)
      }

    }

Got this answer from this post on drupal.org.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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