1

I want to skip an element within Feed me after if a condition passes.

So i am listening to the stepBeforeElementMatch event. Within this event i would have something like this:

if($event->feedData['status'] == "false") {

    // dont import, skip me
}

I couldn't find any way how i would skip this specific element. I am wondering if there is a way to even skip an element?

  • 1
    I have been in the same situation, and it's not entirely trivial. The problem is that quite a few of FeedMe's events doesn't actually take the values changed in the event into account once the event has returned (with a couple of exceptionns). In the end, after dissecting the whole flow of a FeedMe imports, I resolved to doing a couple of small changes to the plugin code and made a pull request. I can post an answer with some pointers. It's quite easy fixes. – KSP Oct 26 at 15:56
  • 1
    Second thought – skipping elements to import might be a bit easier, I was trying to shield some entries from being deleted, and that proves a bit more tricky. Anyways, will have a look, and most probably can help out. – KSP Oct 26 at 16:17
2

Ok, so this is the relevant code in the service processing the feed, where the stepBeforeElementMatch is being called:

        // Fire an 'onStepBeforeElementMatch' event
        if ($this->hasEventHandlers(self::EVENT_STEP_BEFORE_ELEMENT_MATCH)) {
            $event = new FeedProcessEvent([
                'feed' => $feed,
                'feedData' => $feedData,
                'contentData' => $matchExistingElementData,
            ]);

            $this->trigger(self::EVENT_STEP_BEFORE_ELEMENT_MATCH, $event);

            if (!$event->isValid) {
                return;
            }

            // Allow event to modify variables
            $feed = $event->feed;
            $feedData = $event->feedData;
            $matchExistingElementData = $event->contentData;
        }

I have not tested this, but by the looks of it, it should be enough to set the $event->isValid to false in your code after doing the necessary checks that you want.

Be aware that the $event->feedData you are working on is an array representation of your feed data (converted from, XML, JSON or whatever). So you need to do your checks accordingly. At this point, Feed Me has not tried to match against potentially existing elements in Craft. Your example code basically assumes there's a field in the feed element with the name status with the value false. If that's not the case, you could do your checks on whatever you want, before a certain date, etc.

But if it is, something like this would be likely to work (again untested):

if($event->feedData['status'] == "false") {

    $event->isValid = false;
}

If you're looking to skip based on some Craft element, you'd either need to query for it (basically doing some custom matching), or use an event later in the process (I can help you out there too, but maybe it's easier in your case to skip it already here).

  • Thank you for your detailed answer, really appreciate it :) – Christopher Dosin Nov 2 at 16:10
  • Happy to help, hope it worked out! – KSP Nov 2 at 16:52

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.