I'm creating nodes pragmatically but I don't know how to fill field_collection fields. Most of the examples i've seen have field_collections being updated from existing nodes and i'm not entirely certain they can also be used when creating new nodes.
function createNode($Obj){
global $user;
$node = entity_create('node', array(
'type' => 'event',
'uid' => 1,
'status' => 1,
'comment' => 0,
'promote' => 0,
));
$body = "<p>" . (string)$Obj->BuildingCode;
$body .= " " . (string)$Obj->RoomNumber . "</p>";
$body .= "<p>" . (string)$Obj->Description . "</p>";
$entity = entity_metadata_wrapper('node', $node);
$entity->language(LANGUAGE_NONE);
$entity->title = (string)$Obj->ActivityName;
$entity->body->set(array(
'value' => $body,
'summary' => (string)$Obj->Description,
'format' => 'full_html',
));
$entity->field_activityid = (string)$Obj->ActivityId;
$entity->field_location_room = (string)$Obj->RoomNumber;
$entity->field_location_building = (string)$Obj->BuildingCode;
$entity->field_location_address->set(array(
'country' => '',
'thoroughfare' => '',
'premise' => '',
'locality' => '',
'administrative_area' => '',
'postal_code' => '',
));
// $enitity->field_event_organizer->set(array(
// 'field_event_presenter_name' => (string)$Obj->ContactFirstName,
// 'field_event_presenter_title' => "TEST",
// 'field_event_presenter_organizati' => "ORG",
// ));
$values = array(
'field_name' => 'field_event_organizer',
'field_event_presenter_name' => array(
LANGUAGE_NONE => array(array('value' => (string)$Obj->ContactFirstName)),
),
'field_event_presenter_title' => array(
LANGUAGE_NONE => array(array('value' => "PTITLE")),
),
'field_event_presenter_organizati' => array(
LANGUAGE_NONE => array(array('value' => "PORGANI")),
),
);
$entity->save();
}
EDIT:
main collection field:
sub fields:
I tried to add a modified version the code below in hopes it would work for me:
$field_collection_item = entity_create('field_collection_item', array(
'field_name' => 'field_event_organizer'
));
$field_collection_item->setHostEntity('node', $node);
$field_collection_item->field_event_organizer_name = "PROG NAME";
$field_collection_item->save();
$entity->field_event_organizer = $field_collection_item->item_id;
However i got an error EntityMetadataWrapperException: Unknown data property propertyInfo. in EntityStructureWrapper->getPropertyInfo() (line 354 of /Applications/MAMP/htdocs/sites/all/modules/contrib/entity/includes/entity.wrapper.inc).
EDIT: Re-tweaked the answer given to me by SN_26 and it manages to create the node with the field_collection filled in but I get the following error message:
The website encountered an unexpected error. Please try again later.
Status message Registration settings have been saved.
Error messageEntityMetadataWrapperException: There can be only numerical keyed items in a list. in EntityListWrapper->get() (line 1038 of /Applications/MAMP/htdocs/sites/all/modules/contrib/entity/includes/entity.wrapper.inc).
full updated code:
function createNode($Obj){
global $user;
var_dump($Obj);
$node = entity_create('node', array(
'type' => 'event',
'uid' => 1,
'status' => 1,
'comment' => 0,
'promote' => 0,
));
$body = "<p>" . (string)$Obj->BuildingCode;
$body .= " " . (string)$Obj->RoomNumber . "</p>";
$body .= "<p>" . (string)$Obj->Description . "</p>";
$entity = entity_metadata_wrapper('node', $node);
$entity->language(LANGUAGE_NONE);
$entity->title = (string)$Obj->ActivityName;
$entity->body->set(array(
'value' => $body,
'summary' => (string)$Obj->Description,
'format' => 'full_html',
));
$entity->field_activityid = (string)$Obj->ActivityId;
$entity->field_location_room = (string)$Obj->RoomNumber;
$entity->field_location_building = (string)$Obj->BuildingCode;
$entity->field_location_address->set(array(
'country' => '',
'thoroughfare' => '',
'premise' => '',
'locality' => '',
'administrative_area' => '',
'postal_code' => '',
));
$entity->field_event_date[0]->set(array(
'value' => _unix_date_time($Obj->StartDate, $Obj->StartMinute),//Y-m-d H:i:s
'value2' => _unix_date_time($Obj->EndDate, $Obj->EndMinute),
));
$field_collection_item = entity_create('field_collection_item', array(
'field_name' => 'field_event_organizer'
));
$field_collection_item->setHostEntity('node', $node);
$field_collection_item->field_event_organizer_name[LANGUAGE_NONE][0]['value'] = "PROG NAME";
$field_collection_item->save();
$entity->field_event_organizer[LANGUAGE_NONE][0]['value'] = $field_collection_item->item_id;
$entity->save();
}