In field_collection_field_update(), the field collection gets saved. The host entity is only updated for "is_new" field collections, however, the host entity also needs to be set if the host's revision id has changed.
Currently, the any modules implementing hooks (hook_entity_update, etc) for field collection item entities do not have access to the host entity and entity type unless its explicitly set. The reason that they do not have access to the host entity is that the EFQ in fetchHostDetails() is executed for the new field collections revision id which has not been written to the field data tables for the host entity. The field tables are not updated until after field_collection_field_update has returned the new field collection ids.

Example call stack for a Commerce Order entity with a Field collection field.

  • New order revision is triggered

    Order tables are updated with the new order revision.
    • Order's field_attach_update() is triggered.
      • Field collection: field_collection_field_update() fires
        • Triggers a new field collection revision via a save() with skipping the host entity save.
          • hook_entity_update for the field collection is triggered
            • Another contrib module (ie cer) attempts to get the field collection's hostEntity(), but fc's EFQ returns an empty result since fc revision id has not been updated in the field tables linking the fc revision to the host entity's new revision.
          • hook_entity_update for the field collection completed.
        • Field collection save completed.
      • Field collection: field_collection_field_update() returns fc's item id and new revision id
    • Order's field_attach_update() writes field data to field tables. Relationship between field collection and host updated - new field collection revision related to the new order revision.
  • Order save completed.
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

recrit’s picture

Status: Active » Needs review
FileSize
605 bytes

The attached patch updates field_collection_field_update with a call to FieldCollectionItemEntity::updateHostEntity() for existing field collections.

recrit’s picture

Priority: Normal » Major

bumping priority since this could be the cause of many field collection saving issues.

recrit’s picture

Issue summary: View changes
NancyDru’s picture

My line numbers are way off from yours. You show line 460, but I found that function at 987 for beta8.

I pulled the dev release and they match.

recrit’s picture

Patches are applied to the HEAD version: 7.x-1.x-dev.

NancyDru’s picture

Looks good to me. My errors are gone.

Boobaa’s picture

Status: Needs review » Reviewed & tested by the community

I have run into four Undefined index: entity keys in entity_extract_ids() notices when saving a node with three field_collection_items on it. The patch from #1 applied fine (with some offsets) to field_collection-7.x-1.0-beta8, which solved my problem. I have tried field_collection-7.x-1.0-beta8+10-dev based on #5, which caused the notice appearing twice for every field_collection_item (6 times for my three items). The patch in #1 applies cleanly to this, and solves the problem for me.

This is Ready To Be Committed, since it solves the problem at least for two different person.

  • jmuzz committed c0a4425 on 7.x-1.x authored by recrit
    Issue #2458891 by recrit: Update host entity for revision changes
    
jmuzz’s picture

Status: Reviewed & tested by the community » Fixed

Thanks. Nice catch.

Funny the updateHostEntity function got pulled from the entity translation patch and committed early but this part didn't make it.

Amir Simantov’s picture

Worked also for me.

Status: Fixed » Closed (fixed)

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

kaizerking’s picture

Status: Closed (fixed) » Active

WSOD on again editing the field collection after applying this patch

NancyDru’s picture

I do that a lot and have had no problem.

jmuzz’s picture

Status: Active » Closed (fixed)

The patch in this issue has already been applied. There's no need to apply it yourself. Simply download the latest dev version of the module, that way you'll be sure to have the function that it's trying to call.

If you're still having problems then please open a new issue. Feel free to reference this one if you are really sure it is the cause. Also if it's a WSOD please provide the specific error and backtrace. If you don't know how to get information about a white screen error this page may help: https://www.drupal.org/node/158043