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.
Problem/Motivation
Right now, Feeds has no way of handling any errors besides displaying a message and incrementing a failed counter. It'd be nice to give modules a chance to respond to individual item errors for logging or notification purposes.
Proposed resolution
Add a hook_feeds_item_error()
module_invoke within the FeedsProcessor Exception handler that gives modules a chance to log or otherwise handle these exceptions.
Remaining tasks
User interface changes
None.
API changes
Add hook_feeds_item_error()
:
/**
* Invoked when a feed item throws an error during import.
*
* @param FeedsSource $source
* FeedsSource object that describes the source that is being imported.
* @param $entity
* The entity object that has just been saved.
* @param array $item
* The parser result for this entity.
* @param Exception $e
* The exception that was thrown.
*/
function hook_feeds_item_error(FeedsSource $source, $entity, $item, Exception $exception) {
$tmp = array_merge($item, array('error' => $exception->getMessage()));
$filename = 'public://feeds_errors_' . $source->importer()->id . '-' . date('Y-m-d', time()) . '.csv';
$file_is_new = !file_exists($filename);
$file = fopen($filename, 'a');
if ($file_is_new) {
fputcsv($file, array_keys($tmp));
}
fputcsv($file, $tmp);
fclose($file);
}
Data model changes
None.
Comment | File | Size | Author |
---|---|---|---|
#2 | 2692233-2-give_third_party_modules_the.patch | 1.76 KB | nvahalik |
Comments
Comment #2
nvahalik CreditAttribution: nvahalik at Acro Commerce commentedComment #3
nvahalik CreditAttribution: nvahalik at Acro Commerce commentedComment #4
twistor CreditAttribution: twistor as a volunteer commentedThis is very interesting. I've been looking for a way to separate out error handling. It would need to extend to updated, deleted, etc. I'm not sure if this is the correct approach, but it does look promising.
Comment #5
nvahalik CreditAttribution: nvahalik at Acro Commerce commented@twistor, can you be a little more specific about what you mean about it needing to "extend to updated, deleted, etc."?
Comment #6
MegaChriz CreditAttribution: MegaChriz as a volunteer commentedRelated issue in which is explained what the current possibilities are for handling with failed imports: #2565069-2: Email alert on failure.
My guess is that twistor means that it would be great if you could react not only to the event of failed items, but also when
$state->created
,$state->updated
,$state->deleted
,$state->unpublished
and$state->blocked
are incremented.This could be implemented by adding a method to the FeedsState class. The method would then be responsible for incrementing the particular state and invoking the hook.
Something like this in FeedsState:
Not sure if this is right as parsers and fetchers also use a FeedsSource object and these do not have item nor entity.
And then something like this in FeedsProcessor:
Comment #7
MegaChriz CreditAttribution: MegaChriz as a volunteer commented