0
votes

I just found a script to programatically delete a field collection to a specific node :

<?php
  $node = node_load(1);
  $field_collection_item_value = $node->field_page_collection1[LANGUAGE_NONE][0]['value']; // Take field collection item value.
  entity_delete_multiple('field_collection_item', array($field_collection_item_value)); // Delete field collection item.
?>

Unfortunately as i see it , it only delete first field collection, I need to select which one i want to delete.

Here is my structure :

Multiple field collection who have : a reference to another node and two selects

I have the reference nid in the url so I can use it, but I don't have any idea how to select the right field collection with that.

Thanks

1

1 Answers

2
votes

Try to use this:

$node = node_load(1);
$searhed_nid = '2';
$field_page_collection1 = field_get_items('node', $node, 'field_page_collection1');
foreach ($field_page_collection1 as $item) {
  $field_collection = entity_load_single('field_collection_item', $item['value']);
  $fc_item_wrapper = entity_metadata_wrapper('field_collection_item', $field_collection);
  // lets take name the field with ref field_ref_nid.
  $field_val = $fc_item_wrapper->field_ref_nid->raw();
  if ($field_val == $searhed_nid) {
    $field_collection->delete();
  }
}