I have two Node Content Types type_a and type_b I have a field called as field_author_profile in type_a which is entity reference field to type_b with cardinality unlimited.
How do I update field_author_profile programatically.
I also need to Check if value already exists in the field , if exists then append the new value while updating the field value or else just assign the value.
I tried the below variants but seems none worked .
$author_object->set('field_author_profile', $author_profile_nid); $author_object->field_author_profile = ['target_id' => $author_profile_nid]; $author_object->field_author_profile->entity->target_id->value = $author_profile_nid; $author_object->field_my_entity_reference[] = ['target_id' => $author_profile_nid]; $author_object->field_author_profile->target_id = $author_profile_nid; $author_object->get('field_author_profile')->setValue(['target_id' => $author_profile_nid]);
Saved the node after this by using
$author_object->save();