Saturday, 30 May 2015

How to add field collections to node through code in Drupal 7?

//Load the node details:
$nid = 10;
$node = node_load($nid);
$language = $node->language;
//Create Field Collection Item:
$field_collection_item = entity_create('field_collection_item', array('field_name' => 'field_collection_1'));
        $field_collection_item->setHostEntity('node', $node);
        $field_collection_item->field_one[$language][0]['value'] = t('Value One');
        $field_collection_item->field_two[$language][0]['value'] = t('Value Two');
        $field_collection_item->save();
        node_save($node);
 

No comments:

Post a Comment

Your comment is so valuable as it would help me in my growth of knowledge.