public function EntityResource::patchRelationship in JSON:API 8
Updates the relationship of an entity.
Parameters
\Drupal\Core\Entity\EntityInterface $entity: The requested entity.
string $related_field: The related field name.
mixed $parsed_field_list: The entity reference field list of items to add, or a response object in case of error.
\Symfony\Component\HttpFoundation\Request $request: The request object.
Return value
\Drupal\jsonapi\ResourceResponse The response.
File
- src/
Controller/ EntityResource.php, line 681
Class
- EntityResource
- Process all entity requests.
Namespace
Drupal\jsonapi\ControllerCode
public function patchRelationship(EntityInterface $entity, $related_field, $parsed_field_list, Request $request) {
$related_field = $this->resourceType
->getInternalName($related_field);
if ($parsed_field_list instanceof Response) {
// This usually means that there was an error, so there is no point on
// processing further.
return $parsed_field_list;
}
/* @var \Drupal\Core\Field\EntityReferenceFieldItemListInterface $parsed_field_list */
$this
->relationshipAccess($entity, 'update', $related_field);
// 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 */
$field_list = $entity->{$related_field};
$is_multiple = $field_list
->getFieldDefinition()
->getFieldStorageDefinition()
->isMultiple();
$method = $is_multiple ? 'doPatchMultipleRelationship' : 'doPatchIndividualRelationship';
$this
->{$method}($entity, $parsed_field_list);
$this
->validate($entity);
$entity
->save();
return $this
->getRelationship($entity, $related_field, $request, 204);
}