I have a profile2 with a field collection field that allows multiple values.

My rule component simply accepts a field collection as a parameter, and deletes the field collection entity with that ID. Executing via passing the field collection ID will remove it when I view the profile, but displays the error: Notice: Trying to get property of non-object in field_collection_field_get_entity() (line 1589 of /Users/amorton/Sites/edge/edge/sites/all/modules/contrib/field_collection/field_collection.module). Examining the profile in devel shows that the field collection is still listed. The entry is gone from the field_collection_item table in the DB, but still exists in the field_data and field_revision tables.

I have also tried the "Remove an item from a list" action. If I have 2 field collection items saved to the profile, executing the rule using the ID for one will remove BOTH when viewing the profile. However, examining the profile in devel shows that the one I did not remove is still attached. Checking the field's table in the database shows that the item's revision ID is now null. Manually setting it back to match the field ID will bring it back, but I have to clear the cache first.

#4 1968976-delete-action-4.patch930 bytesmortona2k
PASSED: [[SimpleTest]]: [MySQL] 132 pass(es). View
#3 1968976-delete-action-2.patch449 byteskscheirer
PASSED: [[SimpleTest]]: [MySQL] 132 pass(es). View


mortona2k’s picture

I wrote a custom action that uses field_collection's delete function, and everything worked fine:

// Function hook_rules_action_info() not shown

function MODULE_action_delete_item($item) {
kscheirer’s picture

Version: 7.x-1.0-beta5 » 7.x-1.x-dev
Assigned: Unassigned » kscheirer
Status: Active » Needs review

Thanks mortona2k - I added the function above as a patch. Do we need to add a hook_rules_action_info() as well? I'm not too familiar with rules/actions.

kscheirer’s picture

449 bytes
PASSED: [[SimpleTest]]: [MySQL] 132 pass(es). View

Sorry, patch attached.

mortona2k’s picture

930 bytes
PASSED: [[SimpleTest]]: [MySQL] 132 pass(es). View

Here's your patch with the hook_rules_action_info() filled in. Ultimately, what this patch does is add a rules action "Delete field collection item" in the "Field Collection group"

If possible, I think it would be better to have everything just work when you select "Delete Entity". I wonder if it doesn't due to Field Collection's entity implementation, like something wonky in the delete function not properly extending the parent function.

This patch does work, but you'd have to know to look for this action when you might expect to just use the default delete entity. We may want consider putting this action into the "Entities" rule group, which would place "delete field collection item" right next to "delete entity", making it more obvious.

mortona2k’s picture

Here's rules' code for deleting entities. Not sure why the $wrapper->delete() doesn't use field collection's delete function.

 * Action: Delete entities.
function rules_action_entity_delete($wrapper, $settings, $state, $element) {
  try {
  catch (EntityMetadataWrapperException $e) {
    throw new RulesEvaluationException($e->getMessage(), array(), $element);

The documentation mentions overriding the save/delete functions so that the entity api can pick up the changes, but I don't see what's going wrong:

That's it. You can use the entity_save/delete/create() functions to manage your entity, or if you prefer, you can use the methods of your 'entity class', e.g. $entity->save().

To modify the default save/delete/create behavior, extend the provided controller class and override the methods accordingly. [UNCLEAR:]However, if you make use of an entity class you may also directly override the methods provided by your entity class, as the API functions entity_save() and entity_delete() pick that up.

jibize’s picture

After deleting a field collection through Rules, I was also having the problem with the Host Entity still showing the Field collection but with empty fields inside, like if all the fields within the "supposed to be removed" Field collection have been reset to null but the FC was still here. Applying patch #4 and using the action "delete field collection" worked for me, thanks!

jsibley’s picture

Any progress on putting this into a release? Thanks.

dgdrupler’s picture

Issue summary: View changes

I Applied this patch and I am still getting the empty fields when I select "delete field collection item" looping from a field collection list in rules. I'm running the latest dev version.

kristofferwiklund’s picture

Status: Needs review » Needs work
Issue tags: +Needs reroll
milos.kroulik’s picture

Using patch #4 makes no difference for me. The reference to the field collection entity is still there.

Kris77’s picture

I have the same thing as @dgdrupler: "I Applied this patch and I am still getting the empty fields when I select "delete field collection item" looping from a field collection list in rules."