In my scenario:
Click remove button when there is only last one item, it can be removed actually and create new empty item, save it. After saving, edit and save again. It will get this error

Exception: The host entity cannot be changed. in FieldCollectionItemEntity->updateHostEntity()

.

This is my solution:

@@ -453,15 +453,14 @@ class FieldCollectionItemEntity extends Entity {
         }
         $result = $query->execute();
         list($this->hostEntityType, $data) = each($result);
-
         if ($this->isInUse()) {
-          $this->hostEntityId = $data ? key($data) : FALSE;
+          $this->hostEntityId = $data ? end(array_keys($data)) : FALSE;
           $this->hostEntityRevisionId = FALSE;
         }
         // If we are querying for revisions, we get the revision ID.
         else {
           $this->hostEntityId = FALSE;
-          $this->hostEntityRevisionId = $data ? key($data) : FALSE;
+          $this->hostEntityRevisionId = $data ? end(array_keys($data)) : FALSE;
         }
       }
       else {

It's modified in protected function fetchHostDetails(). In the $data variable, it will get multiple values, the previous code gets the first value, but it's not the newest ID, my code is changing to get last value.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

webdrips’s picture

This worked for me...thanks!

juan.obando’s picture

This worked for me as well. Thanks!!!

andyblake’s picture

thanks v v v much

wmnnd’s picture

Status: Active » Reviewed & tested by the community

This worked great for fixing the issue described in https://www.drupal.org/node/2382089
Thanks a lot!

jmuzz’s picture

Status: Reviewed & tested by the community » Needs work

You didn't see any php warnings when using this solution?

" This array is passed by reference because it is modified by the function. This means you must pass it a real variable and not a function returning an array because only actual variables may be passed by reference. "

http://php.net/manual/en/function.end.php

neerajskydiver’s picture

Status: Needs work » Needs review
FileSize
847 bytes

yes, this solution works, but generates the php warnings.
so,
end() is setting the internal pointer of an array to its last element.
by using the current(), this returns current element in an array, in case of empty this returns false.

so, this will resolve the issue.

Slown’s picture

Hi All,

I commented on the indicated line but is this really the right solution? Anyway it works.

giupenni’s picture

This solution works but only partially.

In multilanguage site works only for the nodes with default language

jmuzz’s picture

Status: Needs review » Needs work

See #5.

giupenni’s picture

@jmuzz:
I have this warning:
Strict warning: Only variables should be passed by reference in FieldCollectionItemEntity->fetchHostDetails() (line 316 of /home/myuser/www/mysite/public_html/sites/all/modules/contrib/field_collection/field_collection.entity.inc).

I think this is related to: https://www.drupal.org/node/2833061

judapriest’s picture

Status: Needs work » Needs review

The patch proposed and deployed there https://www.drupal.org/node/2382089#comment-11679969 seems to be same as the solution intented here.

Shouldn't this one be closed as duplicate ? I'm asking for another check, just to be sure.

Status: Needs review » Needs work

The last submitted patch, 6: 2292639-6.patch, failed testing.