Closed (fixed)
Project:
Feeds
Version:
8.x-3.0-alpha2
Component:
Code
Priority:
Normal
Category:
Feature request
Assigned:
Unassigned
Reporter:
Created:
10 Aug 2018 at 12:00 UTC
Updated:
15 May 2020 at 10:03 UTC
Jump to comment: Most recent, Most recent file
Comments
Comment #2
megachrizI 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
megachrizComment #4
abu-zakham commentedComment #5
megachrizThe 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#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 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@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_PRESAVEorFeedsEvents::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 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 39The 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@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
megachrizCoding standards fixes.
Comment #13
megachrizCommitted #11.
Comment #14
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)