Programmatically creating/deleting/modifying field-collection

Enough Said , visit the link: http://rajanmayekar.com/blog/programmatically-creating-deleting-modifyin...

One change wrt to deleting Field Collections is as follows:

<?php
$node
= node_load($node_id);
$field_collection_item_value = array();
foreach (
$node->field_collection_field[LANGUAGE_NONE] as $key => $value) {
   
$field_collection_item_value[] = $value['value'];
}
entity_delete_multiple('field_collection_item', array($field_collection_item_value));
?>

However, deleting field collections would not remove their entries from the node where they are referenced, hence the nodes would provide log of the field collection object not being available when Drupal tries to load the deleted reference. Hence a small code snippet below becomes handy in resolving this issue:

<?php
$node
= node_load($node_nid);
foreach(
$node->field_collection_field[LANGUAGE_NONE] as $key=>$value) {
 
$fc entity_load_single("field_collection_item",$value['value']);
  if(!
is_object($fc)){
    print
$value['value']." does not exist \n";
    unset(
$node->field_collection_field[LANGUAGE_NONE][$key]);
  }
}
node_save($node);
?>