commit 00c5460a3d249e4e3e7b82be0626d8ed85d96a9b
Author: Raphael Spencer <raphael@skyleafdesign.com>
Date:   Thu Jan 2 19:40:07 2014 -0600

    Issue #2162393 by micromegas: Add support for entity_create().

diff --git a/flag.module b/flag.module
index 39d7f3d..b86ddcc 100644
--- a/flag.module
+++ b/flag.module
@@ -33,6 +33,7 @@ function flag_entity_info() {
       // The following tells EntityAPI how to save flaggings, thus allowing use
       // of Entity metadata wrappers (if present).
       'save callback' => 'flagging_save',
+      'creation callback' => 'flagging_create',
     ),
   );
 
@@ -73,6 +74,35 @@ function flagging_load($flagging_id, $reset = FALSE) {
 }
 
 /**
+ * Creates an unsaved flagging object for use with $flag->flag().
+ *
+ * @param $values
+ *   An array of values as described by the entity's property info. Only 
+ *   'flag_name' or 'fid' must be specified, since $flag->flag() does the rest.
+ *
+ * @return
+ *   An unsaved flagging object containing the property values.
+ */
+function flagging_create($values = array()) {
+  $flagging = (object) array();
+  
+  if (!isset($values['flag_name'])) {
+    if (isset($values['fid'])) {
+      // Add flag_name, determined from fid.
+      $flag = flag_get_flag(NULL, $values['fid']);
+      $values['flag_name'] = $flag->name;
+    }
+  }
+
+  // Apply the given values.
+  foreach ($values as $key => $value) {
+    $flagging->$key = $value;
+  }
+
+  return $flagging;
+}
+
+/**
  * Saves a flagging entity.
  *
  * For a new flagging, throws an exception is the flag action is not allowed for
