How can I programmatically know when a certain feed has no items?
What if in case a feed has no new items? Would this be possible?

Regards.

Comments

Yorgg’s picture

Issue summary: View changes

Rephrasing the question.

megachriz’s picture

Component: Feeds Import » Code
Issue summary: View changes
Status: Active » Fixed

When implementing the hook hook_feeds_after_import() in a custom module you can inspect the statistics of the import result by calling state(FEEDS_PROCESS) on the source object. If $state->created is zero, there were no new items created.

Example:

/**
 * Implements hook_feeds_after_import().
 */
function mymodule_feeds_after_import(FeedsSource $source) {
  $state = $source->state(FEEDS_PROCESS);
  if ($state->created == 0) {
    // Now new items created.
  }
}

There isn't an easy way to know beforehand if the feed has new items, though it is possible to check that with custom code:

  1. Call preview() on the source object.
  2. Loop through all items. For each item check for an existing ID. If there is an existing one, the item is not new. You will need to examine the code of FeedsProcessor::process() to find out how an existing ID for an item can be get.

Status: Fixed » Closed (fixed)

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