I need to check if a multi-valued reference field (referencing other nodes) in a node has a specific value already present to avoid duplicates.
here is the code:
$portFolioNode = \Drupal\node\Entity\Node::load( $form_state->getValues()['portfolios'] );
//now save location id to reference field.
//$portFolioNode->field_portfolio_locations[] = ['target_id' => $form_state->getValues()['location_nid'] ];
// how can I check if location_nid is present already?
$portFolioNode->field_portfolio_locations[] = $form_state->getValues()['location_nid'] ;
$portFolioNode->save();
Any help will be appreciated greatly. Thanks!