I get the following error when viewing a node with a field collection that had items deleted using entity_delete('field_collection_item', $field_collection_item_value);

Notice: Trying to get property of non-object in field_collection_field_get_entity() (line 1589 of /****/modules/field_collection/field_collection.module).

I addressed the issue by checking if $entity->revision_id was set.

<?php
if(isset($entity->revision_id)) {
		if ($entity->revision_id != $item['revision_id']) {
		  // A non-default revision is a referenced, so load this one.
		  $entity = field_collection_item_revision_load($item['revision_id']);
		}
	}
?>

If I deleted the item then why would it be trying to look for a revision id?

Thanks,
TJ

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

Striky2’s picture

Hi, +1

I get the exact same issue after manually deleting the old revisions attached to a node. Whilst editing the latest version of the node, all the contents of field collections are empty.

GoZ’s picture

Version: 7.x-1.0-beta5 » 7.x-1.x-dev
Status: Active » Needs review
FileSize
1.32 KB

I get same issue.
I make patch from tjferre solution.

This patch is part of the #1day1patch initiative.

Status: Needs review » Needs work
GoZ’s picture

My patch fail tests, but trying to fix it, i found existing patch for revisions which fix this issue (and issue for revisions) : http://drupal.org/node/1316162#comment-6827200

Simon Georges’s picture

Should this be closed as a duplicate, then?

GoZ’s picture

Status: Needs work » Closed (fixed)

You are right. I thought i have did it

jaroslaw.kaminski’s picture

Priority: Normal » Major
Status: Closed (fixed) » Active

Hello,
I have same problem.

I'm trying to delete programmatically some field_collection items and I've getting same error as OP.

  $service = field_get_items('node', $node, 'field_our_services');
   foreach ($service as $service_item) {
     var_dump($service_item);
     entity_delete_multiple('field_collection_item', array($service_item));
     var_dump(entity_load('field_collection_item',array($service_item)));

this is my code. There is no difference if you use $service_item or $service_item['value'] in entity_delete_multiple.

var_dumps giving ( after several code runs too):

array(2) {
  ["value"]=>
  string(2) "60"
  ["revision_id"]=>
  string(2) "95"
}

array(0) {
}



array(2) {
  ["value"]=>
  string(2) "61"
  ["revision_id"]=>
  string(2) "96"
}

array(0) {
}

so entity_delete_multiple remove items only partial. Deleting main object of entity but some of things are still on node.
In previous versions of field_collections evrything was fine, I think revision system complicated situation.

vbouchet’s picture

Hi,

I have updated the patch to make it workable with the last version of field_collection (7.x-1.0-beta5).

For an unknown reason, field_collection_field_get_entity is sometimes called with unexisting items. That means the field_collection_item_load retrieve FALSE and create a Notice on the next line because of ->revision_id.

dmsmidt’s picture

Previous patch failed to apply for me on the beta5.

Manually patched against latest -dev.

brandy.brown’s picture

#9 worked perfectly for me with beta5. Thank you!

ivanhelguera’s picture

+1 here as well :-)

ssoulless’s picture

Status: Needs review » Active

After apply patch in #9 the notice disappeared. But when I try to create a new item, or update the field collection I get this error

EntityMetadataWrapperException: Unable to get the data property field_cantidad_salida as the parent data structure is not set. in EntityStructureWrapper->getPropertyValue() (line 438 of /home/servilla/public_html/servillantasgiraldo.com.co/sites/all/modules/entity/includes/entity.wrapper.inc).

jmuzz’s picture

If I understand correctly, this is about using entity_delete and entity_delete_multiple to remove field collections, right? The reason field_collection_field_get_entity gets called is that the host entity still has a reference to the deleted field collection after the entity is deleted this way. I really don't think a patch for field_collection_field_get_entity is the right approach. That reference should be removed instead. I posted a patch that should accomplish this in #2186689: throws EntityMetadataWrapperException after use entity_delete() for delete an item within field collection. Please give it a try. Maybe one of these can be closed as a duplicate.

heddn’s picture

heddn’s picture