loading a field collection with either entity_load() or field_collection_item_load(). both return same structure and both have all the hostEntity properties as NULL.

is this expected? is there a simple API call to get host entity_id or do i need to access db directly?

Comments

johnpitcairn’s picture

The hostEntity property looks to be protected. So $entity->hostEntity() won't work in some contexts. I think.

For example, I'm finding that I can use $entity->hostEntity() successfully on the entity in hook_entity_presave(), but not when I have manually loaded the entity somewhere else via entity_load(). Bah.

Bitvark’s picture

The entity_load_single() function seems to work, for me.
Otherwise I think you can try with $entity->hostEntityId() and then loading the hosting entity with entity_load().
It happens sometimes that while hostEntity is unaccessible, hostEntityId is available.

kla2t’s picture

In my template.php, I needed to pull content from the host node into a field collection field, and neither hostEntity() nor hostEntityId() were accessible to anonymous users.
As a workaround, I ended up with loading the node through menu_get_object() to get the required content.
This works, at least, for simple node pages. Might be useful for someone else since this problem seems to persist.

jmuzz’s picture

Issue summary: View changes
Status: Active » Fixed

It is normal. The host information isn't loaded by default when loading a field collection item. It will be loaded with a call to fetchHostDetails() which is called if necessary by any of the host information functions.

Hope this helps.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.