// Validate the received data before saving.
$this->
validate($entity);
try { $entity->
save();
$this->logger->
notice('Created entity %type with ID %id.',
['%type' =>
$entity->
getEntityTypeId(), '%id' =>
$entity->
id()]);
// 201 Created responses return the newly created entity in the response
// body. These responses are not cacheable, so we add no cacheability
// metadata here.
$headers =
[];
if (in_array('canonical',
$entity->
uriRelationships(), TRUE
)) { $url =
$entity->
toUrl('canonical',
['absolute' => TRUE
])->
toString(TRUE
);
$headers['Location'
] =
$url->
getGeneratedUrl();
} return new ModifiedResourceResponse($entity, 201,
$headers);
} catch (EntityStorageException
$e) { throw new HttpException(500, 'Internal Server Error',
$e);
} } /**
* Responds to entity PATCH requests.
*
* @param \Drupal\Core\Entity\EntityInterface $original_entity
* The original entity object.
* @param \Drupal\Core\Entity\EntityInterface $entity
* The entity.
*
* @return \Drupal\rest\ModifiedResourceResponse
* The HTTP response object.
*
* @throws \Symfony\Component\HttpKernel\Exception\HttpException
*/