Skip to content
Snippets Groups Projects

Issue #3304654: Improve correctness of `EntityResource::replaceRelationshipData()` and `::removeFromRelationshipData()`

Open Issue #3304654: Improve correctness of `EntityResource::replaceRelationshipData()` and `::removeFromRelationshipData()`
1 file
+ 2
4
Compare changes
  • Side-by-side
  • Inline
@@ -687,15 +687,13 @@ public function replaceRelationshipData(ResourceType $resource_type, EntityInter
@@ -687,15 +687,13 @@ public function replaceRelationshipData(ResourceType $resource_type, EntityInter
/** @var \Drupal\Core\Field\EntityReferenceFieldItemListInterface $resource_identifiers */
/** @var \Drupal\Core\Field\EntityReferenceFieldItemListInterface $resource_identifiers */
$resource_identifiers = $this->deserialize($resource_type, $request, ResourceIdentifier::class, $related);
$resource_identifiers = $this->deserialize($resource_type, $request, ResourceIdentifier::class, $related);
$internal_relationship_field_name = $resource_type->getInternalName($related);
$internal_relationship_field_name = $resource_type->getInternalName($related);
// According to the specification, PATCH works a little bit different if the
// relationship is to-one or to-many.
/** @var \Drupal\Core\Field\EntityReferenceFieldItemListInterface $field_list */
/** @var \Drupal\Core\Field\EntityReferenceFieldItemListInterface $field_list */
$field_list = $entity->{$internal_relationship_field_name};
$field_list = $entity->{$internal_relationship_field_name};
$field_definition = $field_list->getFieldDefinition();
$field_definition = $field_list->getFieldDefinition();
$is_multiple = $field_definition->getFieldStorageDefinition()->isMultiple();
$is_multiple = $field_definition->getFieldStorageDefinition()->isMultiple();
$method = $is_multiple ? 'doPatchMultipleRelationship' : 'doPatchIndividualRelationship';
$method = $is_multiple ? 'doPatchMultipleRelationship' : 'doPatchIndividualRelationship';
$this->{$method}($entity, $resource_identifiers, $field_definition);
$this->{$method}($entity, $resource_identifiers, $field_definition);
$this->validate($entity);
static::validate($entity, [$field_definition->getName()]);
$entity->save();
$entity->save();
$requires_response = static::relationshipResponseRequiresBody($resource_identifiers, ResourceIdentifier::toResourceIdentifiersWithArityRequired($field_list));
$requires_response = static::relationshipResponseRequiresBody($resource_identifiers, ResourceIdentifier::toResourceIdentifiersWithArityRequired($field_list));
return $this->getRelationship($resource_type, $entity, $related, $request, $requires_response ? 200 : 204);
return $this->getRelationship($resource_type, $entity, $related, $request, $requires_response ? 200 : 204);
@@ -799,7 +797,7 @@ public function removeFromRelationshipData(ResourceType $resource_type, EntityIn
@@ -799,7 +797,7 @@ public function removeFromRelationshipData(ResourceType $resource_type, EntityIn
}
}
// Save the entity and return the response object.
// Save the entity and return the response object.
static::validate($entity);
static::validate($entity, [$field_list->getFieldDefinition()->getName()]);
$entity->save();
$entity->save();
return $this->getRelationship($resource_type, $entity, $related, $request, 204);
return $this->getRelationship($resource_type, $entity, $related, $request, 204);
}
}
Loading