Download & Extend

Trying to get property of non-object in field_collection_field_get_entity()

Project:Field collection
Version:7.x-1.x-dev
Component:Code
Category:bug report
Priority:major
Assigned:Unassigned
Status:active

Issue Summary

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

Comments

#1

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.

#2

Version:7.x-1.0-beta5» 7.x-1.x-dev
Status:active» needs review

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

This patch is part of the #1day1patch initiative.

AttachmentSizeStatusTest resultOperations
field_collection-non-object-field_collection_field_get_entity-1880312-2.patch1.32 KBIdleFAILED: [[SimpleTest]]: [MySQL] 25 pass(es), 7 fail(s), and 6 exception(s).View details | Re-test

#3

Status:needs review» needs work

The last submitted patch, field_collection-non-object-field_collection_field_get_entity-1880312-2.patch, failed testing.

#4

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

#5

Should this be closed as a duplicate, then?

#6

Status:needs work» closed (fixed)

You are right. I thought i have did it

#7

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.

<?php
  $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.

nobody click here