Skip to content

Instantly share code, notes, and snippets.

@michaellopez
Created October 31, 2018 05:55
Show Gist options
  • Save michaellopez/0b3952d21cec90549e31cce469e60888 to your computer and use it in GitHub Desktop.
Save michaellopez/0b3952d21cec90549e31cce469e60888 to your computer and use it in GitHub Desktop.
diff --git a/field_collection.entity.inc b/field_collection.entity.inc
index 2e8a11b..ed8dfb7 100644
--- a/field_collection.entity.inc
+++ b/field_collection.entity.inc
@@ -291,24 +291,29 @@ class FieldCollectionItemEntity extends Entity {
protected function fetchHostDetails() {
if (!isset($this->hostEntityId)) {
if ($this->item_id) {
+ $in_use = $this->isInUse();
+
// For saved field collections, query the field data to determine the
// right host entity.
$query = new EntityFieldQuery();
$query->fieldCondition($this->fieldInfo(), 'revision_id', $this->revision_id);
- if (!$this->isInUse()) {
+ if (!$in_use) {
$query->age(FIELD_LOAD_REVISION);
}
$result = $query->execute();
- list($this->hostEntityType, $data) = each($result);
-
- if ($this->isInUse()) {
- $this->hostEntityId = $data ? key($data) : FALSE;
- $this->hostEntityRevisionId = FALSE;
- }
- // If we are querying for revisions, we get the revision ID.
- else {
+ if ($result) {
+ $this->hostEntityType = key($result);
+ $data = current($result);
+ end($data);
+
+ // The entity ID or revision ID if we are querying for revisions.
+ $id = key($data);
+ $this->hostEntityId = $in_use ? $id : FALSE;
+ $this->hostEntityRevisionId = $in_use ? FALSE : $id;
+ }
+ else {
+ // No host entity available yet.
$this->hostEntityId = FALSE;
- $this->hostEntityRevisionId = $data ? key($data) : FALSE;
}
}
else {
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment