2

Is there a way on Feedme to not import an entry if it doesn't have a match on a specific field?

e.g. If there is no entry that it matches up to, don't import it.

Here's the current code I have:

Event::on(Process::class, Process::EVENT_STEP_BEFORE_ELEMENT_SAVE, function (FeedProcessEvent $event) {
    $myField=$event->feedData['myfieldname'];
    if ($myField == null){
        $event->element->isValid=false;
    }
});

The above processes the data getting loaded in. Here's code for processing data after it's parsed.

Event::on(Fields::class, Fields::EVENT_AFTER_PARSE_FIELD, function(FieldEvent $event) {
    if ($event->fieldHandle['merchants'] == null) {
    $event->isValid=false;
    }
});
2
+50

isValid should be set on the $event variable, not on $event->element:

Event::on(Process::class, Process::EVENT_STEP_BEFORE_ELEMENT_SAVE, function (FeedProcessEvent $event) {
    $myField=$event->feedData['myfieldname'];
    if ($myField == null){
        $event->isValid=false;
    }
});

I haven't tested but looking at the feed-me source code, this should work.

  • Thanks for the answer. I got it working, but I'm looking to access the field's in FeedMe. I have a field in FeedMe called "Merchants" but some of imported entries won't get matched up to anything, so I'm trying to stop them being saved if they don't match up. – JMKelley Nov 26 '20 at 15:10
  • Updated the question above. – JMKelley Nov 26 '20 at 15:17
0

Updated

After a lot of tinkering - I finally found out how to check if the field is matching/existing and if nothing matches - Skip adding this element.

Event::on(
Process::class, 
Process::EVENT_STEP_BEFORE_ELEMENT_SAVE, 
function (craft\feedme\events\FeedProcessEvent $event) {
  switch($event->feed['name']){
    case 'offers':

      $merchants = $event->element->merchants;
      if (sizeof($merchants) === 0) {
          $event->isValid=false;
      }
    break;
  }
});
  • Looks like the only thing that differs from nstCactus' answer is your additional check code? IMO that doesn't really improve the answer as the key is in the $event->isValid line which was pointed out in that answer. (You also didn't mention in the question that $merchants is supposed to be an array?) – RitterKnight Nov 30 '20 at 16:40
  • Sure, I've reimbursed the internet points. – JMKelley Nov 30 '20 at 18:42

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.