Programmatically add an entity reference field or a role

Submitted by barnettech on

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

For a single use

$group_content1->group_roles->target_id = $role_id