From b4b55221cb03879266d4aafd0d702f7d4e3e1b43 Mon Sep 17 00:00:00 2001
From: Alex Barth <alex_b@53995.no-reply.drupal.org>
Date: Wed, 22 Dec 2010 01:46:46 +0000
Subject: [PATCH] Fix creating new item info on the fly if there is no existing
 one.

---
 feeds.module               | 17 +++++++++++------
 plugins/FeedsProcessor.inc |  4 +++-
 2 files changed, 14 insertions(+), 7 deletions(-)

diff --git a/feeds.module b/feeds.module
index 71e28f3b..80a9f5a5 100644
--- a/feeds.module
+++ b/feeds.module
@@ -495,7 +495,7 @@ function feeds_node_insert($node) {
  */
 function feeds_node_update($node) {
   // Node produced by source.
-  feeds_item_info_update($node, $node->nid);
+  feeds_item_info_save($node, $node->nid);
 
   // Source attached to node.
   if ($importer_id = feeds_get_importer_id($node->type)) {
@@ -534,7 +534,7 @@ function feeds_taxonomy_term_insert($term) {
  * Implements hook_taxonomy_term_update().
  */
 function feeds_taxonomy_term_update($term) {
-  feeds_item_info_update($term, $term->tid);
+  feeds_item_info_save($term, $term->tid);
 }
 
 /**
@@ -558,7 +558,7 @@ function feeds_user_insert(&$edit, $account, $category) {
  * Implements hook_user_update().
  */
 function feeds_user_update(&$edit, $account, $category) {
-  feeds_item_info_update($account, $account->uid);
+  feeds_item_info_save($account, $account->uid);
 }
 
 /**
@@ -764,12 +764,17 @@ function feeds_item_info_insert($entity, $entity_id) {
 }
 
 /**
- * Updates an item info object in he feeds_item table.
+ * Inserts or updates an item info object in he feeds_item table.
  */
-function feeds_item_info_update($entity, $entity_id) {
+function feeds_item_info_save($entity, $entity_id) {
   if (isset($entity->feeds_item)) {
     $entity->feeds_item->entity_id = $entity_id;
-    drupal_write_record('feeds_item', $entity->feeds_item, array('entity_type', 'entity_id'));
+    if (feeds_item_info_load($entity->feeds_item->entity_type, $entity_id)) {
+      drupal_write_record('feeds_item', $entity->feeds_item, array('entity_type', 'entity_id'));
+    }
+    else {
+      feeds_item_info_insert($entity, $entity_id);
+    }
   }
 }
 
diff --git a/plugins/FeedsProcessor.inc b/plugins/FeedsProcessor.inc
index f1a5e856..60662f97 100644
--- a/plugins/FeedsProcessor.inc
+++ b/plugins/FeedsProcessor.inc
@@ -120,8 +120,10 @@ abstract class FeedsProcessor extends FeedsPlugin {
           }
           else {
             $entity = $this->entityLoad($source, $entity_id);
-            if ($this->loadItemInfo($entity)) {
+            // If an existing item info can't be loaded, create one.
+            if (!$this->loadItemInfo($entity)) {
               $this->newItemInfo($entity, $source->feed_nid, $hash);
+              $entity->feeds_item->entity_id = $entity_id;
             }
           }
           $this->map($source, $parser_result, $entity);
-- 
GitLab