From 6ca9cf2ed050492fab6836f934c83f902de971cf Mon Sep 17 00:00:00 2001
From: James Sansbury <james.sansbury@lullabot.com>
Date: Wed, 20 Feb 2013 10:44:59 -0500
Subject: [PATCH] Issue #1922484: Handle updates and increment counters in
 flag destination class.

---
 flag.inc |   21 +++++++++++++++++++++
 1 file changed, 21 insertions(+)

diff --git a/flag.inc b/flag.inc
index 6008c3f..c268cca 100644
--- a/flag.inc
+++ b/flag.inc
@@ -54,6 +54,23 @@ class MigrateDestinationFlagSimple extends MigrateDestination {
    *  successful. FALSE on failure.
    */
   public function import(stdClass $entity, stdClass $row) {
+    // Updating previously-migrated content?
+    if (isset($row->migrate_map_destid1)) {
+      if (isset($entity->fcid)) {
+        if ($entity->fcid != $row->migrate_map_destid1) {
+          throw new MigrateException(t("Incoming id !id and map destination id !destid don't match.",
+            array('!id' => $entity->fcid, '!destid' => $row->migrate_map_destid1)));
+        }
+      }
+      else {
+        $entity->fcid = $row->migrate_map_destid1;
+      }
+    }
+    // Otherwise, clear out the fcid, just to be safe.
+    else {
+      unset($entity->fcid);
+    }
+
     if (isset($entity->timestamp)) {
       $entity->timestamp = Migration::timestamp($entity->timestamp);
     }
@@ -67,6 +84,10 @@ class MigrateDestinationFlagSimple extends MigrateDestination {
       $return = drupal_write_record('flag_content', $entity);
     }
     if ($return) {
+      // Increment the number of updated or inserted records by checking the
+      // result of drupal_write_record.
+      ($return == SAVED_NEW) ? $this->numCreated++ : $this->numUpdated++;
+
       // Update the flag_counts table.
       $count = db_select('flag_content')
         ->fields('flag_content')
-- 
1.7.10.4

