Breaking

Followers

Friday, 16 June 2017

Programmatically create and update field collection item for the node with entity API

To attach or create field-collection item for node having node id say 16.

$node= node_load(16);
$field_collection_val= entity_create('field_collection_item', array('field_name'=> 'field_collection1'));
// Attach to the node
$field_collection_value->setHostEntity('node', $node); .
$field_collection_value->field_collection1[LANGUAGE_NONE][0]['value'] = 'your fetch data here/some data here';
// Save field-collection item.
$field_collection_value->save();


To modify/update (load/edit/save) values from existing field collection item of node having node id 16

$nid = 16;
$node = node_load($nid);
// Get field collection item value.
$field_collection_item_value = $node->field_collection1[LANGUAGE_NONE][0]['value'];
 // Delete field collection item.
entity_delete_multiple('field_collection_item', array($field_collection_item_value));


No comments:

Post a Comment