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


I tried both of the previous while accessing the actual nid value



$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;

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)
  • What is an example value for $oc_wrapper->field_call_site_ref->value()?
    – sonfd
    Apr 23, 2018 at 17:46
  • Is it an int? an array? an object?
    – sonfd
    Apr 23, 2018 at 18:11
  • It's an object. Specifically the node object of the node being referenced. Apr 23, 2018 at 18:12
  • You get that same error when you do $nc_wrapper->field_comm_relationship = $oc_wrapper->field_call_site_ref->value(); $nc_wrapper->save();? Is field_comm_relationship a multivalue field?
    – sonfd
    Apr 23, 2018 at 18:21
  • If I run that code I get an SQL error: PDOException: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '), ('node', '713866', '1158104', 'comm', 'field_site_handset_serial', 'und', 'Ar' at line 1: INSERT INTO {field_data_field_comm_relationship} (entity_type, entity_id, revision_id, bundle, delta, language, field_comm_relationship_nid) VALUES...` and then it lists a BUNCH of values. Apr 23, 2018 at 18:26

1 Answer 1


After updating the value in your entity metadata wrapper you need to be sure to also call the save() method to save your change. See this documentation on drupal.org.

Since you have an object, and not an id, the following should work.

// Set value for a single value field.
$nc_wrapper->field_comm_relationship = $oc_wrapper->field_call_site_ref->value();


// Set value for a multivalue field. Make sure to pass an array of objects.
$nc_wrapper->field_comm_relationship = [$oc_wrapper->field_call_site_ref->value()];


// Append an additional value to a multivalue field.
$nc_wrapper->field_comm_relationship[] = $oc_wrapper->field_call_site_ref->value();

and finally

// Save the host entity.

If you have ids, check out this answer. You can get an id instead of an object from the entity metadata wrapper like $oc_wrapper->field_call_site_ref->raw() instead of ->value();

Additional info for entity metadata wrappers can be found in this Death to Field Arrays! article.

  • Thanks for the thoughtful response. I do call save() later in my code, but it's not making it that far before a fatal error. I have updated my question. Apr 23, 2018 at 17:32
  • I've updated this answer. I hope this resolves this for you. I think your issues were because your field is a multivalue field and not a single value field.
    – sonfd
    Apr 23, 2018 at 18:42
  • 1
    $nc_wrapper->field_comm_relationship[] = $oc_wrapper->field_call_site_ref->value(); that got it. Thanks! Apr 23, 2018 at 19:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.