Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
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.
Comment | File | Size | Author |
---|---|---|---|
#6 | 2292639-6.patch | 847 bytes | neerajskydiver |
Comments
Comment #1
webdrips CreditAttribution: webdrips commentedThis worked for me...thanks!
Comment #2
juan.obando CreditAttribution: juan.obando commentedThis worked for me as well. Thanks!!!
Comment #3
andyblake CreditAttribution: andyblake commentedthanks v v v much
Comment #4
wmnnd CreditAttribution: wmnnd commentedThis worked great for fixing the issue described in https://www.drupal.org/node/2382089
Thanks a lot!
Comment #5
jmuzz CreditAttribution: jmuzz commentedYou 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
Comment #6
neerajskydiver CreditAttribution: neerajskydiver as a volunteer and at Valuebound commentedyes, 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.
Comment #7
Slown CreditAttribution: Slown commentedHi All,
I commented on the indicated line but is this really the right solution? Anyway it works.
Comment #8
giupenni CreditAttribution: giupenni commentedThis solution works but only partially.
In multilanguage site works only for the nodes with default language
Comment #9
jmuzz CreditAttribution: jmuzz commentedSee #5.
Comment #10
giupenni CreditAttribution: giupenni commented@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
Comment #11
judapriestThe 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.