This is forked from #1186826: Delete orphaned field_collection_item entities when they are (programatically) removed from the host entity.

Some have found the reverse situation can cause issues - when programmatically deleting the field_collection_item entity that the fields that referenced did not have their values cleared.

Comments

jamsilver’s picture

From http://drupal.org/node/1186826#comment-5111484:

I'm getting still getting this issue after the patch. When deleting a FieldCollectionItemEntity directly, the reference in the node's collection field remains and triggers an EntityMalformedException.

I thought I would be able to just use
$field_collection_item->delete();
or
if ($ids = field_collection_field_item_to_ids($node->field_my_field[LANGUAGE_NONE)) {
entity_delete_multiple('field_collection_item', $ids);
}

But because of the stale references in the collection field that triggers :
"EntityMalformedException: Missing bundle property on entity of type field_collection_item. in entity_extract_ids() (line 7389..."

I can avoid this by manually removing the collectionfield item from the collection field like
unset($node->field_my_field[LANGUAGE_NONE]);
field_attach_update('node', $node);

But shouldn't that be done automatically?

From http://drupal.org/node/1186826#comment-5196922

I've got this problem as well and made the following obervation - maybe someone else is able to work his magic with this hint:

So I've got a node I cannot edit anymore, receiving the error:
EntityMalformedException: Missing bundle property on entity of type field_collection_item. in entity_extract_ids() (line 7405 of /srv/www/htdocs/iav.com/data/includes/common.inc).

Now the node id is 8034 and the field_collection_item for that content type is field_global_video. So I checked the database-field: field_data_field_global_video, looked up the line of the entity_id 8034 and found the value 192 in the field: field_global_video_value.

So with the 192 in mind I went over to the field_collection_item table but couldn't find any item_id 192. Since I know that this should be a field_global_video in my case, I just inserted the line manually via PHPMyAdmin.

INSERT INTO `tablename`.`field_collection_item` (`item_id`, `field_name`) VALUES ('192', 'field_global_video');

And voila... the node is editable again. Now this is certainly not a solution, but maybe a hint.

Daniel Wentsch’s picture

I've got the same problem and no solution, but I can confirm Lerain's method of adding those values manually to the database worked for me, too.

It might be of interest that I'm using i18n and the problem occured after adding a node translation.

Daniel Wentsch’s picture

Good news :)
I managed to track the bug, at least in my case.
It is indeed related to i18n. Whenever a translated node was deleted the original node got corrupted.
Luckily there's already a available patch for this:
#1315432: Deleting a translated node with a collection also deletes the collection entity ..

Worked out for me.

melissavdh’s picture

I am also getting this problem, and I'm not using any node translations so I think it's for a different reason to klickreflex. I just have some very simple code; a node is programmatically created and the user has the option to edit the node and add/edit/delete any field collections through an interface I have written (not the standard drupal node UI). When the user edits this node and deletes a field collection, that field collection item deletes successfully but the reference from the node to the field collection still remains. I then get the "EntityMalformedException" error mentioned above when I try and edit this node through the drupal node UI.

I am also able to prevent this occurring by manually unsetting the reference from the node to the field collection item before I delete the field collection, as shown above in the first linked comment (unset($node->field_my_field[LANGUAGE_NONE]);). However I do think that this should be done automatically.

Dave Reid’s picture

So I created a patch in #1399746: Fatal error if a referenced field collection is deleted to at least prevent entities with deleted referenced field collections from causing this fatal so you can still edit them, but automatically removing orphaned field collections is still a good addition to make.

jmuzz’s picture

Issue summary: View changes
Status: Active » Postponed (maintainer needs more info)

There is a bug where using entity_delete or entity_delete_multiple will leave the reference to its field collection in its host. There is a patch for it posted here. #2186689: throws EntityMetadataWrapperException after use entity_delete() for delete an item within field collection I believe it should be working when using $field_collection_item->delete(), but please say something if the patch does not solve the problem.

jmuzz’s picture

Status: Postponed (maintainer needs more info) » Closed (duplicate)