0

I am trying to dynamically create a field collection for a node.

Field collection item (field_markers) is used to create a point or a polygon for a map content type.

During debugging I found that my code create an entry for all the member elements of field collection but it does not create or update a record for field collection itself.

I have referred to https://stackoverflow.com/questions/6242018/dynamically-add-a-field-collection-in-drupal-7-by-script but was not able to resolve my problem.

Following is my code so far.

//Create field collection
$field_collection_item = entity_create('field_collection_item', array('field_name' => 'field_markers'));

//Create fields related to field collection
$field_collection_item->field_title['und'][0]['value'] = "Brian 22";
$field_collection_item->field_summary['und'][0]['value'] = "Test entity creation.";
$field_collection_item->field_summary['und'][0]['format'] = "filtered_html";

$lat = -35.3241665257384;
$lon = 139.8046875;

$field_collection_item->field_coordinates['und'][0]['wkt'] = 'POINT ('.$lon.' '.$lat.')';
$field_collection_item->field_coordinates['und'][0]['geo_type'] = 'point';
$field_collection_item->field_coordinates['und'][0]['lat'] = $lat;
$field_collection_item->field_coordinates['und'][0]['lon'] = $lon;
$field_collection_item->field_coordinates['und'][0]['left'] = $lon;
$field_collection_item->field_coordinates['und'][0]['top'] = $lat;

$field_collection_item->field_coordinates['und'][0]['right'] = $lon;
$field_collection_item->field_coordinates['und'][0]['bottom'] = $lat;

//attach field collection to map content type
$field_collection_item->setHostEntity('map', $node);
// Save the entity.
$field_collection_item->save(TRUE);
1

I found the problem, it was related to the workflow of content publishing (specific to my site ). Field collection item is created after a user with required permissions approve the content.

The code in question works and could be helpful for anyone else trying to create a field collection item containing a geofield.

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.