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.
Hi,
I was wondering if there is a way to hook into feeds after importing each item (or maybe after all of them).
What I need (as I mentioned in another ticket) is to import nodes and then create corresponding group nodes (Group module).
I was thinking I could do a normal node import and then hook into it with some custom code to create the group nodes programmatically.
I can see a postprocess function in the Processor but not sure if that has access to the feed import data (e.g all entities created, updated etc).
Thanks!
Comment | File | Size | Author |
---|---|---|---|
#11 | interdiff-2991955-06-11.txt | 578 bytes | MegaChriz |
#11 | feeds-presave-and-skip-2991955-11.patch | 14.47 KB | MegaChriz |
| |||
#6 | feeds-presave-and-skip-2991955-6.patch | 14.68 KB | MegaChriz |
Comments
Comment #2
MegaChriz CreditAttribution: MegaChriz commentedI have been struggling with this issue as well. I was doubting if I should either introduce a new event or pass the item to the entity instead. With the first solution, you'll need to subscribe to an event. With the second solution, you can use the regular entity hooks (for example
hook_entity_update()
) and on it request for the item being processed, which would only be available when processing. As I had a need to also be able to manipulate the entity before validating I had to at least introduce some kind of prevalidate event, as I did not found a hook that is invoked before entities are validated.See the patch for work in progress that includes both solutions. The patch is named 'entityreference-version' because I created it on top of an other patch, so it may not apply completely.
Comment #3
MegaChriz CreditAttribution: MegaChriz commentedComment #4
abu-zakham CreditAttribution: abu-zakham at Vardot commentedComment #5
MegaChriz CreditAttribution: MegaChriz at WebCoo commentedThe patch in #2 no longer applies.
A separate issue has been opened for the prevalidate event: #3054851: Dispatch an event before validation.
Comment #6
MegaChriz CreditAttribution: MegaChriz at WebCoo commented#3054851: Dispatch an event before validation is committed. Back to this issue.
The attached patch adds two events: one for presaving and one for postsaving an entity. Besides test coverage for these two events, test coverage for most other events dispatched by Feeds is added as well. It also includes a (hidden) feature to skip importing item while presaving an entity. Just throw an EmptyFeedException. Skip importing an item while in the presave phase is a feature that exists in the D7 version of Feeds as well. So basically this is a port of some features from the D7 version.
Comment #7
anawillem CreditAttribution: anawillem commentedI have the patched feed files with the eeds-presave-and-skip-2991955-6.patch and testing the feed both with the hook_entity_presave and the hook_entity_postsave (just in case they made a difference) and could not get those fields to change/populate
Just in case it made a difference, I also tried naming the hook 'hook_feeds_entity_presave' and those fields did not seem to get populated with that change either...
No errors in the watchdog. It does not seem to be getting triggered at all as I have a drupal_set_message at the end, and see nothing.
The tests were for feeds of type RSS/Atom.
Please let me know if there is more information I can give.
The function I created is basically:
Comment #8
MegaChriz CreditAttribution: MegaChriz at WebCoo commented@anawillem
Thanks for trying the patch! What is added are not hooks, but events. So in order to use them, you'll need to have an event subscriber that subscribes to
FeedsEvents::PROCESS_ENTITY_PRESAVE
orFeedsEvents::PROCESS_ENTITY_POSTSAVE
.Example:
The purpose of the patch is to be able to react on saving an entity in the context of a feed import, but also to be able to prevent an entity from being saved based on the parsed feed item data. This data is not available during regular entity hooks.
Comment #9
anawillem CreditAttribution: anawillem commentedThank you for responding! Very clear. I have something up with a few edits as I respond to errors, but am now getting an error that I am not entirely understanding:
TypeError: Argument 1 passed to Drupal\edf_tweaks\EventSubscriber\EdfTweaksAlertsFeedEventSubscriber::presave() must be an instance of Drupal\edf_tweaks\EventSubscriber\Drupal\feeds\Event\EntityEvent, instance of Drupal\feeds\Event\EntityEvent given in Drupal\edf_tweaks\EventSubscriber\EdfTweaksAlertsFeedEventSubscriber->presave() (line 39
The first lines of the file I am working with are:
Instead of working with the $item, I am working with the $node and then just doing a node_save();
Similar as last time, I am not getting any of the messages triggered. This is after adding the service to the module.services.yml file.
Thank you again!
Comment #10
MegaChriz CreditAttribution: MegaChriz at WebCoo commented@anawillem
When you specify the full namespaced class name in an use statement, you only need use the non-namespaced class name in the rest of the code.
Example:
From the code example above, pay attention to the following line:
It only states "EntityEvent" here, not "Drupal\feeds\Event\EntityEvent", because the full namespaced class is already in the "use" statement earlier in the code:
Sidenote: if for some reason you wouldn't use the "use" statement, the full namespaced class should start with a backslash, since you are in a namespace:
Without the backslash, PHP assumes the class is called "Drupal\mymodule\EventSubscriber\Drupal\feeds\Event\EntityEvent". Starting with a backslash let's PHP know it should ignore the declared namespace.
For better readability of the code, I recommend to always use the "use" statement near the start of the file. I only explained this to explain why you got that TypeError error.
You also don't have to include use statements for classes you don't use in your class. So from your code example above, you would only need the following lines in the "use" section of your class:
Comment #11
MegaChriz CreditAttribution: MegaChriz as a volunteer commentedCoding standards fixes.
Comment #13
MegaChriz CreditAttribution: MegaChriz as a volunteer commentedCommitted #11.
Comment #14
anawillem CreditAttribution: anawillem commentedWell, after some jostling around of code, I have something that works well with your committed #11. For posterity, the final code is like this:
Thank you, @MegaChriz!
Comment #16
Bohus Ulrychupdate post
Note: this helped (from test ... FeedsSubscriber.php)