0

I've a content type having filed collection item (3 fields of 5 sets). Created a custom module form, when user submits that form, it should create a node with all 5 set field collection items.

So far tried the following code. it's working for 1st set but not for all.

$values = array(
  'type' => 'ct_machine_name', // Content Type machine name
  'uid' => $uid,
  'status' => 1,
  'promote' => 0,
  'title' => 'xxxx',
);
$entity = entity_create('node', $values);
$ewrapper = entity_metadata_wrapper('node', $entity);
$ewrapper->save();
$node = node_load($entity->nid);

$fc_val= entity_create('field_collection_item', array('field_name' => 'field_fc_mcnename'));
// Attach to the node
$fc_val->setHostEntity('node', $node);
$fc_val->field_field1[LANGUAGE_NONE][0]['value'] = $val1;
$fc_val->field_field2[LANGUAGE_NONE][0]['value'] = $val2;
$fc_val->field_field3[LANGUAGE_NONE][0]['value'] = $val3;

// Save field-collection item.
$fc_val->save();

The above code working. But I need to loop through it and save all field collection items in an user submitted order.

I tired as follows, but doesn't work. (example)

for ($i=0; $i<=4; $i++) {
$fc_val->field_field1[LANGUAGE_NONE][$i]['value'] = $val1;
$fc_val->field_field2[LANGUAGE_NONE][$i]['value'] = $val2;
$fc_val->field_field3[LANGUAGE_NONE][$i]['value'] = $val3;
}
$fc_val->save();

Also tried

for ($i=0; $i<=4; $i++) {
$fc_val->field_field1[LANGUAGE_NONE][0]['value'] = $val1;
$fc_val->field_field2[LANGUAGE_NONE][0]['value'] = $val2;
$fc_val->field_field3[LANGUAGE_NONE][0]['value'] = $val3;
$fc_val->save();
}

Any inputs or suggestions would be much appreciated.

0

Try This:
Example for field1 and repeat it with other fields.


$entity_fc_val = entity_metadata_wrapper('field_collection_item', $fc_val);
$entity_fc_val->field_field1->set(array('value' => $val1));

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.