Programmatically add an entity reference field or a role

Submitted bybarnettech onTue, 11/02/2021 - 15:49

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