I know I can update (or set a value for) an entity reference field in Drupal 7 with code similar to the following.

 $node['field_code_used_by'][LANGUAGE_NONE][0]['target_id'] = $id;

I want to programmatically set a value for my user entity reference field in Drupal 8.

I tried with $node->field_code_used_by->setValue(1); and $node->field_code_used_by->entity->value = 1; but neither of them worked.

How can I set the value for an entity reference field?

4 Answers 4


You need to use code similar to the following.

    $node = Node::load($nid);     
    $node->field_code_used_by->target_id = $user_id;

For a multiple-value field, to add the value to the end of the list, use the following code.

$node->field_code_used_by[] = ['target_id' => $user_id];

Another way is setting the entity property with the entity object, as in the following code.

    $node = Node::load($nid);
    $user = \Drupal\user\Entity\User::load(1);
    $node->field_code_used_by->entity = $user;
  • 3
    This doesn't seem to work unless the parent node already exists. When assigning the target_id or entity property to a new, unsaved parent node, the relationship is not saved. (Possibly also doesn't work when assigning to an NULL entity ref field, but i haven't tested that.) Anyone know how to address this case? Nov 28, 2018 at 18:52
  • 1
    @aaronbauman did you got any solution to it ?
    – echo
    Jan 9, 2019 at 9:45
  • 1
    @echo i don't remember how or if i worked around this. If my description of the behavior is accurate, this is a bug. If there isn't an open issue on drupal.org for it, there should be Jan 9, 2019 at 18:09
  • 1
    @aaronbauman drupal.org/project/paragraphs/issues/2707017#comment-11872624 helped me to make that work with new node. So doesn't seems to be a bug.
    – echo
    Jan 10, 2019 at 4:38

Like in D7 the main property of a reference field in D8 is still the target id:

$node->field_code_used_by->target_id = $user_id;

The entity property is a computed value. If you put an object in this value, drupal will convert it back to the id before saving it to the dababase.

Multivalue fields

Add an item to a multivalue field:

$node->field_code_used_by[] = ['target_id' => $user_id];

The property target_id is not necessary, because it is the main property and used by default. So for most fields you can add a value like in a simple php array, which is easy to remember:

$node->field_code_used_by[] = $user_id;
  • 2
    How about for entity reference fields that allow multiple values?
    – Brian
    Jan 15, 2017 at 14:43
  • 2
    How can I check if a value already exist in the multi value field @4k4 ? Dec 21, 2019 at 17:24

You should be aware of the difference between a single value field and a multiple value field. Code below shows how. Source: https://stefvanlooveren.me/blog/how-programmatically-update-entity-reference-field-drupal-8

$imageIds = [
foreach($imageIds as $index => $fid) {
  if($index == 0) {
    $node->set('field_article_images', $fid);
  } else {
      'target_id' => $fid,
  • 1
    Oh man I tested this without images and for a single value field and it works too. I found the blog using google I didn't scroll down to your answer. Mar 2, 2020 at 16:59
  • 1
    This may be a change with later versions of Drupal 8, but I need to include the 'target_type' and 'target_id' to be able to save references to users.
    – MD3
    Feb 5, 2021 at 6:55

For multi value fields you can use:

$node = Node::load(212);
$multi_value = $node->get('field_NAME')->getValue();
$multi_value[] =['target_id' => 101];
$node->set('field_NAME', $multi_value);

For single value fields you can use:

$node->set('field_NAME',['target_id' => 101]);

Your Answer

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