// According to the specification, you are only allowed to POST to a
// relationship if it is a to-many relationship.
/** @var \Drupal\Core\Field\EntityReferenceFieldItemListInterface $field_list */
$field_list =
$entity->
{$internal_relationship_field_name};
/** @var \Drupal\field\Entity\FieldConfig $field_definition */
$field_definition =
$field_list->
getFieldDefinition();
$is_multiple =
$field_definition->
getFieldStorageDefinition()->
isMultiple();
if (!
$is_multiple) { throw new ConflictHttpException(sprintf('You can only POST to to-many relationships. %s is a to-one relationship.',
$related));
} $original_resource_identifiers = ResourceIdentifier::
toResourceIdentifiersWithArityRequired($field_list);
$new_resource_identifiers =
array_udiff( ResourceIdentifier::
deduplicate(array_merge($original_resource_identifiers,
$resource_identifiers)),
$original_resource_identifiers,
[ResourceIdentifier::
class, 'compare'
] );
// There are no relationships that need to be added so we can exit early.
if (empty($new_resource_identifiers)) { $status =
static::
relationshipResponseRequiresBody($resource_identifiers,
$original_resource_identifiers) ? 200 : 204;
return $this->
getRelationship($resource_type,
$entity,
$related,
$request,
$status);
}