We have hooks for field collection item insert, update and delete. But when the revision feature is enabled for the host entity and when we remove field collection items from the host entity, it will not delete the field collection item from the database. Instead, the field collection items will be archived, for the host entity's old revisions. So, the hook_field_collection_item_delete hook will be invoked.
We need a hook invoked during this archive action. Otherwise, it is very difficult to loop through each field collection items on parent node form submit and perform the sub-actions.
Here is a sample use case:
For our project, based on a field collection, we create nodes programmatically using hook_field_collection_item_presave and hook_field_collection_item_update hooks. We need to delete/archive those contents if the referenced field collections are removed from the parent entity. With revision enabled for parent node, the hook_field_collection_item_delete will not be triggered while deleting field collection items from the node form. This is because, field collection items are not actually deleted, instead it is being archived for old revisions' reference.
With this new hook, we can delete/archive our sub, dependent contents using archived items IDs.
| Comment | File | Size | Author |
|---|---|---|---|
| #5 | field-collection-archive-hook-2872350-5.patch | 2.39 KB | ronino |
| #4 | field-collection-archive-hook-2872350-4.patch | 1.79 KB | sarvab |
| #2 | archive_hook-2872350-1.patch | 1.43 KB | Joel James |
Comments
Comment #2
Joel James commentedHere is the patch for new hook
field_collection_items_archive.Comment #3
Joel James commentedComment #4
sarvab commentedI'm updating the patch to work more like other entity hooks: pre-save, insert, update, delete. I ran into this issue while expecting hook_field_collection_delete to run but it didn't. With this I can use the same code for both hook_delete and hook_archive which I think makes a bit more sense.
Comment #5
ronino commentedThis hook is great. I use it to move files referenced in fields of archived field collection items to the private:// filesystem.
I reworked #4 a bit to make sure that the item cache is cleared after the hook has been called. I also corrected the documented hook's name and reworked documentation.
Comment #6
ram4nd commentedComment #8
ram4nd commented