Last updated November 16, 2012. Created by dale42 on March 10, 2012.
Edited by eosrei. Log in to edit this page.
In this example a function adds an entry to a node with a field collection named field_activity_log.
<?php
function foo_log_activity($node, $message, $uid = NULL) {
// Load the controller class file.
module_load_include('inc', 'entity', 'includes/entity.controller');
// Use the currently logged in user if a $uid argument is not defined.
global $user;
if (empty($uid)) {
$uid = $user->uid;
}
// Grab the current time in the format required by our date field. We need
// gmdate() here to get a time in UTC which is how everything is stored
// internally.
$now = gmdate('Y-m-d H:i:s');
// Setup the values in the structure expected by the field_collection entity.
$values = array(
'field_name' => 'field_activity_log',
'field_activity_log_date' => array(
LANGUAGE_NONE => array(array('value' => $now)),
),
'field_activity_log_message' => array(
LANGUAGE_NONE => array(array('value' => $message)),
),
'field_activity_log_user' => array(
LANGUAGE_NONE => array(array('uid' => $uid)),
),
);
$entity = entity_create('field_collection_item', $values);
// Attach the field_collection entity to the application node. This has to
// happen separately so that it actually works -- you can't just specify
// this via the $values array.
$entity->setHostEntity('node', $node);
// Save the entity. Since it's attached to the right application node, this
// will both create the field_collection entity and update the application
// node to point to the new field_activity_log record.
$entity->save();
}
?>
Comments
what about multi value field
what about multi value field collection ? how the delta is set ??
Run the code
Run the code "...$entity->setHostEntity('node', $node);" twice. I've got.
detail
What would the $node array contain.
Would you please tell the minimum information?
J2R
$node should be a full
$node should be a full fledged node object (like you'd get from node_load)
Sean Reiser
http://resume.seanreiser.com