The attached patch adds a hook hook_feeds_before_import() as discussed, such that a rules integration could use it to customize the imported entities before saving + to skip saving.

Based on the hook we could add an event for it + an action for skipping the import. The entity could be manipulated with the usual rules actions else too. :)

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

alex_b’s picture

Status: Needs review » Needs work

Looking good.

I think we should change the hook signature from

hook_feeds_before_import($entity_type, $entity, FeedsProcessor $processor)

to:

hook_feeds_pre_save($source, $entity);

Reasons:

- more consistent with hook_feeds_after_import() which is called on an import level. A theoretical hook_feeds_pre_import() would have to be invoked on an import level, too.
- usage of $source is consistent with most other hooks (see feeds.api.php) [edit: processor can be accessed via feeds_importer($source->id)->processor]
- $entity_type is accessible on $entity->feeds_item->entity_type

fago’s picture

Status: Needs work » Needs review
FileSize
1.72 KB

ok, I've updated the patch accordingly. I named the hook hook_feeds_presave() though to be consistent with hook_entity_presave().

fago’s picture

FileSize
1.7 KB

fixed it.

dasjo’s picture

FileSize
4.47 KB

additions:

  • FeedsProcessor->configDefaults exposes entity_type, so Rules can work with that
  • feeds_action_skip_item marks a feed import item to be skipped

    this is handy for skipping items, but of course feeds will try to import them again.

    we decided to also implement a FeedsEntityProcessor in order to import generic entities and a more specific FeedsMessageProcessor in order to save them as messages using the message module http://drupal.org/project/message

fago’s picture

Unnecessary empty line:

+function feeds_action_skip_item($entity_wrapper) {
+  $entity = $entity_wrapper->value();
+  
+  $entity->feeds_item->skip = TRUE;
+}

Else the patch looks good to me.

fago’s picture

dasjo’s picture

FileSize
4.47 KB

remove unnecessary empty line

dasjo’s picture

FileSize
4.45 KB

sorry, my patch was in the wrong format

fago’s picture

the last patch still has the line? ;)

dasjo’s picture

FileSize
4.22 KB

removed entityType from configDefaults as this caused endless recusion in some cases
also the empty line should be gone now :)

fago’s picture

I took the patch form #10 and added some improvements:
* add the node-type as a bundle to the variable in case of the node processor -> saves you from checking it in rules
* added some more comments and help for the skip action
* fixed the provided variable to skip-saving in rules, so it isn't double-saved

Patch works fine for me.

+    // Add bundle information if the node processor is used.
+    if ($processor instanceof FeedsNodeProcessor) {
+      $config = $processor->getConfig();
+      $info['feeds_import_'. $importer->id]['variables'][$entity_type]['bundle'] = $config['content_type'];
+    }

It would be cleaner if the processor would tell us what the configured bundle is, as this would work for other processors too. I'm not sure though whether it is ok to introduce something like FeedsProcessor::getBundle() in feeds current development state?
Anyway, currently the only entity-type feed supports that has bundles is "node".

fago’s picture

Title: enable modules to customize imports » Rules integration & enable modules to customize imports
Issue tags: +rules integration

improved title

dasjo’s picture

Status: Needs review » Reviewed & tested by the community

just tested, works nicely :)

---

usage:
- create a reaction event rule on "Before saving an item imported via YourFeedsImporter".
- within the rule or any sub-component use the action "Skip import of feeds item" in order to flag the imported item as skipped.

dasjo’s picture

minor re-roll of fago's patch in #11 which adds a check:

< +  $entity->feeds_item->skip = TRUE;
---
> +  if(isset($entity->feeds_item)) {
> +     $entity->feeds_item->skip = TRUE;
> +  }

this prevents error messages when using the action for non-feeds-imported items

febbraro’s picture

Status: Reviewed & tested by the community » Fixed

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

colan’s picture

Thanks. This just hosed the work I was doing over at #1362378: Provide entity post-save hook. ;) I wish I found this earlier, but I suppose that's what happens when the issue queue gets so huge.

It's not necessarily a duplicate, because we still need the post-save hook. The issue is that the signatures are different. I've got:

* @param $processor
*  The Feeds processor that is currently processing the entity.
* @param $entity
*  The entity object that has just been saved.

...and this one does:

* @param $source
*  FeedsSource object that describes the source that is being imported.
* @param $entity
*   The entity object.

However, from the note above, I'm assuming that the processor is still accessible?

processor can be accessed via feeds_importer($source->id)->processor

So if that's the case, we can leave this as is, and just make the other issue about post-saving.

OldAccount’s picture

Marked #1344028: Use Rules to trigger feed importer as duplicate of this issue.