Skip to main content
improved grammar
Source Link
avpaderno
  • 97.9k
  • 15
  • 164
  • 284

Almost you did it, you just only have tiny mistake instead of set 1 to entity you haveYou need to set target_id(I guest 1 is your userid ),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-value field (to, to add the value to the end of the list), use: the following code.

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

another solutionAnother way is set user entity object to value

So first load your user object then set it to entitysetting 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();

Almost you did it, you just only have tiny mistake instead of set 1 to entity you have to set target_id(I guest 1 is your userid ),

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

For multiple value field (to add the value to the end of the list) use:

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

another solution is set user entity object to value

So first load your user object then set it to entity property with

    $node = Node::load($nid);
    $user = \Drupal\user\Entity\User::load(1);
    $node->field_code_used_by->entity = $user;
    $node->save();

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();

Almost you did it, you just only have tiny mistake instead of set 1 to entity you have to set target_id(I guest 1 is your userid ),

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

For multiple value field (to add the value to the end of the list) use:

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

another solution is set user entity object to value

So first load your user object then set it to entity property with

    $node = Node::load($nid);
    $user = \Drupal\user\Entity\User::load(1);
    $node->field_code_used_by->entity = $user;
    $node->save();

Almost you did it, you just only have tiny mistake instead of set 1 to entity you have to set target_id(I guest 1 is your userid ),

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

another solution is set user entity object to value

So first load your user object then set it to entity property with

    $node = Node::load($nid);
    $user = \Drupal\user\Entity\User::load(1);
    $node->field_code_used_by->entity = $user;
    $node->save();

Almost you did it, you just only have tiny mistake instead of set 1 to entity you have to set target_id(I guest 1 is your userid ),

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

For multiple value field (to add the value to the end of the list) use:

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

another solution is set user entity object to value

So first load your user object then set it to entity property with

    $node = Node::load($nid);
    $user = \Drupal\user\Entity\User::load(1);
    $node->field_code_used_by->entity = $user;
    $node->save();
added 198 characters in body
Source Link
Yuseferi
  • 22.2k
  • 22
  • 118
  • 218

Almost you did it, you just only have tiny mistake instead of set 1 to entity you have to set target_id(I thinkguest 1 is your userid ), you should

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

another solution is set user entity object to value

So first load your user object then set it. to entity property with

    $node = Node::load($nid);
    $user = \Drupal\user\Entity\User::load(1);
    $node->field_code_used_by->entity = $user;
    $node->save();

Almost you did it, you just only have tiny mistake instead of set 1 (I think 1 is your userid ), you should set user entity object to value

So first load your user object then set it.

    $node = Node::load($nid);
    $user = \Drupal\user\Entity\User::load(1);
    $node->field_code_used_by->entity = $user;
    $node->save();

Almost you did it, you just only have tiny mistake instead of set 1 to entity you have to set target_id(I guest 1 is your userid ),

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

another solution is set user entity object to value

So first load your user object then set it to entity property with

    $node = Node::load($nid);
    $user = \Drupal\user\Entity\User::load(1);
    $node->field_code_used_by->entity = $user;
    $node->save();
deleted 7 characters in body
Source Link
Yuseferi
  • 22.2k
  • 22
  • 118
  • 218
Loading
Source Link
Yuseferi
  • 22.2k
  • 22
  • 118
  • 218
Loading