From 2597ba07a31049af0295c5f047bafb63e2e2c8f2 Mon Sep 17 00:00:00 2001
From: Carsten Nielsen <carsten@redpill-linpro.com>
Date: Wed, 15 Aug 2012 11:23:56 +0200
Subject: [PATCH] Fix #1281974: Missing bundle property on entity

Populate field_collection_items with correct bundle before saving.
---
 field_collection.module | 14 +++++++++++++-
 1 file changed, 13 insertions(+), 1 deletion(-)

diff --git a/field_collection.module b/field_collection.module
index b6b8a37..749e82d 100644
--- a/field_collection.module
+++ b/field_collection.module
@@ -1293,7 +1293,19 @@ function field_collection_field_widget_embed_validate($element, &$form_state, $c
   $language = $element['#language'];
 
   $field_state = field_form_get_state($field_parents, $field_name, $language, $form_state);
-  $field_collection_item = $field_state['entity'][$element['#delta']];
+  
+  //We have to populate the field_collection_item before we can attach it to the form.
+  if (isset($field_state['entity'][$element['#delta']])) {
+    $field_collection_item = $field_state['entity'][$element['#delta']];
+  }
+  elseif ($form_state['input'][$field_state['array_parents'][0]][$field_state['array_parents'][1]][$element['#delta']]) {
+    $field_collection_item = clone $field_state['entity'][0];
+    foreach ($form_state['input'][$field_state['array_parents'][0]][$field_state['array_parents'][1]][$element['#delta']] as $key => $value) {
+      if (property_exists($field_collection_item, $key)) {
+        $field_collection_item->{$key} = $value;
+      }
+    }
+  }
 
   // Attach field API validation of the embedded form.
   field_attach_form_validate('field_collection_item', $field_collection_item, $element, $form_state);
-- 
1.7.11.3

