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_author_profile[] = ['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();
Edit : The field value was not being rendered in the node edit form, even thought it was present in the database. It was due to the wrong bundle type selected in the entity reference field. Once it is corrected it worked as expected.
This was the working solution.
$author_object->field_author_profile[] = ['target_id' => $author_profile_nid];