Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
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
MegaChriz CreditAttribution: MegaChriz as a volunteer commentedIn 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 CreditAttribution: veronicaSeveryn at Inclind Inc 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 CreditAttribution: veronicaSeveryn at Inclind Inc 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 CreditAttribution: veronicaSeveryn at Inclind Inc commentedUploaded a wrong file. Here's the patch again.
Comment #7
veronicaSeveryn CreditAttribution: veronicaSeveryn at Inclind Inc commentedComment #8
twistor CreditAttribution: twistor as a volunteer commentedThis should be camel case.
Why are we returning $this?
Comment #9
veronicaSeveryn CreditAttribution: veronicaSeveryn at Inclind Inc commentedUpdated a patch - fixed based on the comment above - and re-based it for the current DEV version status.
Comment #11
vijay.mayilsamy CreditAttribution: vijay.mayilsamy commented- Updated a patch by applying the coding standards recommended from the previous test results.
Comment #12
MegaChriz CreditAttribution: MegaChriz as a volunteer commentedThis 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
selva.swamy@gmail.com CreditAttribution: selva.swamy@gmail.com as a volunteer and 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 CreditAttribution: artematem at FFW commentedUsing patch #12 on our live project. Works as expected.
Comment #15
MegaChriz CreditAttribution: MegaChriz as a volunteer commented@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
MegaChriz CreditAttribution: MegaChriz as a volunteer commentedNow with a unit test. Also fixes some coding standard issues.
Comment #18
MegaChriz CreditAttribution: MegaChriz as a volunteer commentedSingle coding standard fix.
Comment #20
MegaChriz CreditAttribution: MegaChriz as a volunteer commentedCommitted #18.
Comment #22
tbatzakas CreditAttribution: 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 CreditAttribution: MegaChriz as a volunteer commented@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 CreditAttribution: webdrips commentedCan someone share their implementation of #12?
@MegaChriz?
@artematem?
@tbatzakas?
Comment #25
webdrips CreditAttribution: 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.