23

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?

30

You need to use code similar to the following.

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

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;
    $node->save();
  • 2
    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? – aaronbauman Nov 28 '18 at 18:52
  • @aaronbauman did you got any solution to it ? – echo Jan 9 '19 at 9:45
  • @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 – aaronbauman Jan 9 '19 at 18:09
  • @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 '19 at 4:38
11

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;
  • How about for entity reference fields that allow multiple values? – Brian Jan 15 '17 at 14:43
  • How can I check if a value already exist in the multi value field @4k4 ? – InspiredCoder Dec 21 '19 at 17:24
0

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

$imageIds = [
  '3',
  '32',
  '50'
];
foreach($imageIds as $index => $fid) {
  if($index == 0) {
    $node->set('field_article_images', $fid);
  } else {
    $node->get('field_article_images')->appendItem([
      'target_id' => $fid,
    ]);
  }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy