Let's say I have a row in CSV, which has a value in the column that will indicate to me that this row shouldn't be imported.
Is there a way to set some kind of indicator that will prevent the row from being saved?
| Comment | File | Size | Author |
|---|---|---|---|
| #18 | interdiff-2772601-17-18.txt | 475 bytes | megachriz |
| #18 | feeds-after-parse-base-2772601-18.patch | 7.91 KB | megachriz |
| #17 | interdiff-2772601-12-17.txt | 7.38 KB | megachriz |
| #17 | feeds-after-parse-base-2772601-17.patch | 7.93 KB | megachriz |
| #12 | feeds-after-parse-base-2772601-12.patch | 2.65 KB | megachriz |
Comments
Comment #2
megachrizIn the D7 version of Feeds, you would do this with the "Keyword filter" plugin from Feeds Tamper, but that module hasn't been ported to D8 yet. Not sure how to accomplish the same in the D8 version.
In the D7 version you could also accomplish this by implementing
hook_feeds_after_parse()(Feeds Tamper implemented this hook also). Not sure if this hook has been ported to the D8 version of Feeds. Some hooks were converted to events. See feeds/src/Event. If you know how the event system works in D8, perhaps you could make use of that.Comment #3
veronicaseveryn commentedWe could have used Feeds Event:: PROCESS to set some value to indicate that the row should be skipped, or similar to EmptyFeedException throw some kind of SkipRowProcessingException that could be caught/handled later in the processing workflow, but looks like none of these options are available yet..
Comment #4
veronicaseveryn commentedHere's a version of what I came up with - I added a BOOL variable on Feed Item ($process_current_row) that will indicate whether the row should be processed further.
This variable will checked inside the process() function of EntityProcessorBase class.
This way, I can check the values I need on the Feed Item during Process Event, and if I decide to skip row processing based on some parameters, I can use
$item->skipRowProcessing(TRUE);I believe I updated all dependent classes... Worked for me. Need someone else take a look at it.
Comment #6
veronicaseveryn commentedUploaded a wrong file. Here's the patch again.
Comment #7
veronicaseveryn commentedComment #8
twistor commentedThis should be camel case.
Why are we returning $this?
Comment #9
veronicaseveryn commentedUpdated a patch - fixed based on the comment above - and re-based it for the current DEV version status.
Comment #11
vijay.mayilsamy commented- Updated a patch by applying the coding standards recommended from the previous test results.
Comment #12
megachrizThis use case is going to be implemented with Feeds Tamper. See also #2937721: Create 'SkipException' classes..
It could perhaps be useful though to make skipping rows easier without Tamper. Attached is a patch for that idea.
Basically you would extend the AfterParseBase class, implement
alterItem()and throw a \Drupal\feeds\Exception\SkipItemException when the item to import should be skipped.Comment #13
kswamy commentedHi,
Can someone review and mark this as RTBC if it is fine? We need to do some validations before the excel is accepted for upload. I believe the latest patch should allow us to achieve that. Or is there some other module in D8 which allows to add custom validations for the fields uploaded via feeds?
Comment #14
artematem commentedUsing patch #12 on our live project. Works as expected.
Comment #15
megachriz@artematem
Thanks for trying! I'm using this patch also with success on a few sites, but it would be useful to have an automated test for this feature.
Comment #17
megachrizNow with a unit test. Also fixes some coding standard issues.
Comment #18
megachrizSingle coding standard fix.
Comment #20
megachrizCommitted #18.
Comment #22
tbatzakas commentedHello, I managed to use SkipItemException for my dataset, but since I need to assign my data to different categories using Tamper's Find/Replace I end up unable to match the source data I want to skip. As far as I understand, this is because Tamper also works with FeedEvents::PARSE stage. Is there a way to access the source info instead of the tampered one?
Comment #23
megachriz@tbatzakas
Yes, you can change the order in which event subscribers are called. Both Feeds Tamper and the AfterParseBase class have set the same priority, which is
FeedsEvents::AFTER. This constant equals-10000, see feeds/src/Event/FeedsEvents:For event subscribers, subscribers with a higher value are called first. So to have your subscriber called first before Feeds Tamper's one, you could simply increase the value with one:
FeedsEvents::AFTER + 1.Override the static method
getSubscribedEvents()in your custom subscriber class to change the priority:Comment #24
webdrips commentedCan someone share their implementation of #12?
@MegaChriz?
@artematem?
@tbatzakas?
Comment #25
webdrips commentedI found this to be enough to get me where I needed to go: https://www.drupal.org/project/feeds/issues/3128379
I'm happy to provide a code example if someone needs it.