I'm creating a new node and attempting to the values of the fields programmatically. I cannot seem to figure out how to set a value for a node reference field, the widget type is autocomplete and it is set to unlimited values.
Here is my code to create the new node and an entity metadata wrapper
// Create the communication node and set the current user as the author
$new_comm = entity_create('node', array('type' => 'comm', 'uid' => $uid));
// create a wrapper for the new communication
$nc_wrapper = entity_metadata_wrapper('node', $new_comm);
// then I set some other field values via the $nc_wrapper
I cannot seem to set the value for the node reference field field_comm_relationship
. I have tried all manner of setting the value including the following ($oc_wrapper
wraps a different node type that also has a node reference):
I tried inserting it straight from the $oc_wrapper
$nc_wrapper->field_comm_relationship = $oc_wrapper->field_call_site_ref->value();
I tried using the set method
$nc_wrapper->field_comm_relationship->set($oc_wrapper->field_call_site_ref->value());
I tried both of the previous while accessing the actual nid
value
$nc_wrapper->field_comm_relationship->set($oc_wrapper->field_call_site_ref->value()->nid);
and
$nc_wrapper->field_comm_relationship = $oc_wrapper->field_call_site_ref->value()->nid;
I tried to make the input an array
$site_nid = ['und' => [ 0 => ['nid' => (int) $oc_wrapper->field_call_site_ref->value()->nid]]];
$nc_wrapper->field_comm_relationship = $site_nid;
I tried using the node_load
function instead of EMW
$node = node_load('node', $nc_wrapper->nid->value());
$site_nid = $oc_wrapper->field_call_site_ref->value()->nid;
$node->field_comm_relationship['und'][0]['nid'] = $site_nid;
node_save($node);
As suggested I do call save()
later in my code, but it's not making it there because it hits a fatal error. For instance if I run this code
$nc_wrapper->field_comm_relationship = (int) $oc_wrapper->field_call_site_ref->value()->nid;
I get this error
EntityMetadataWrapperException: Invalid data value given.
Be sure it matches the required data type and format.
Value at node()->field_comm_relationship: 713511. in EntityMetadataWrapper->set()
(line 122 of /vagrant/sites/all/modules/contrib/entity/includes/entity.wrapper.inc)
$oc_wrapper->field_call_site_ref->value()
?$nc_wrapper->field_comm_relationship = $oc_wrapper->field_call_site_ref->value();
$nc_wrapper->save();
? Isfield_comm_relationship
a multivalue field?