0

I can get the entity reference field node id from my field collection like this:

function mymodule_node_update($node){

    if ($node->type == 'article'){    

    $node_wrapper = entity_metadata_wrapper('node', $node);

        $before = array();

        foreach ($node_wrapper->field_collection_ingredients->value() as $key => $field_collection_wrapper) {

          $before[$key] = $field_collection_wrapper->field_ingredient[LANGUAGE_NONE][0]['target_id'];

        } 
  }

Doing:

$before[$key] = $field_collection_wrapper->original->field_ingredient[LANGUAGE_NONE][0]['target_id'];

Causes an error.


Basically I have an entity reference, which allows unlimited values, inside the the field collection.

When someone removes a value from the entity reference field, I need to know which value was removed as I need the Node ID to update that node that was removed from the entity reference.

  • Did you tried comparing field_collection_ingredients field value for $node->field_collection_ingredients[LANGUAGE_NONE] and $node->original->field_collection_ingredients[LANGUAGE_NONE], this will give exactly which field collection was removed, you can then try loading that field collection and then load $fc->field_ingredient[LANGUAGE_NONE][0]['target_id']; field value to update Node referenced in field_ingredient field. – Yogesh Dec 2 '16 at 12:12
  • @Yogesh this almost worked, except Drupal throws an error when it tried to load the entity reference nid. Seems like once you remove the item, the collection that belongs to that ID gets removed, which causes the error. – No Sssweat Dec 6 '16 at 3:36
  • If your node has revision enabled then field collection is not removed, instead archived. If not, then it is removed – Yogesh Dec 6 '16 at 4:29
  • @Yogesh are you saying that I need to use the revision id? – No Sssweat Dec 6 '16 at 4:44
  • No, I'm just saying that, if your node does not has revision enabled, the field collection might be removed from database, if you remove from node. – Yogesh Dec 6 '16 at 4:46
1

Try comparing field_collection_ingredients field value of $node->field_collection_ingredients[LANGUAGE_NONE] and $node->original->field_collection_ingredients[LANGUAGE_NONE]‌​, this will give exactly which field collection was removed, you can then try loading that field collection (see note below) and then load $fc->field_ingredient[LANGUAGE_NONE][0]['target_id']; field value to update Node referenced in field_ingredient field.

Note: If your node does not has revision enabled, the removed field collection will be removed from Drupal database as well, which will cause the error when trying to load the Field Collection Entity. So you might need to enable revision for your main node, to make sure that if field collection is removed from field_collection_ingredients field, it will not get removed from Database.

0

If i understand your question in the right way field collections create entities. So you can try to use hook_entity_update to track when your field collection entities are updated (i mean when someone deletes values from entity reference field of your field collection) hook_entity_update($entity, 'field_collection_item'). You also can use entity_metadata_wrapper('field_collection_item', $your_field_collection_entity_id) to get data from field collection entity.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.