TransWikia.com

Feedme Skip Entry if No Matching Elements on Field

Craft CMS Asked on December 22, 2020

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 Answers

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.

Correct answer by nstCactus on December 22, 2020

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 (craftfeedmeeventsFeedProcessEvent $event) {
  switch($event->feed['name']){
    case 'offers':

      $merchants = $event->element->merchants;
      if (sizeof($merchants) === 0) {
          $event->isValid=false;
      }
    break;
  }
});

Answered by JMKelley on December 22, 2020

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP