// Verify that the deserialized entity is of the type that we expect to
// prevent security issues.
if ($entity->
getEntityTypeId() !=
$definition['entity_type'
]) { throw new BadRequestHttpException('Invalid entity type'
);
} // POSTed entities must not have an ID set, because we always want to create
// new entities here.
if (!
$entity->
isNew()) { throw new BadRequestHttpException('Only new entities can be created'
);
} $this->
checkEditFieldAccess($entity);
// 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 =
[];