diff --git a/core/modules/rest/src/Plugin/ResourceBase.php b/core/modules/rest/src/Plugin/ResourceBase.php index b40a03109b..e91afd9da0 100644 --- a/core/modules/rest/src/Plugin/ResourceBase.php +++ b/core/modules/rest/src/Plugin/ResourceBase.php @@ -130,6 +130,10 @@ public function routes() { // Expose one route per available format. $format_route = clone $route; $format_route->addRequirements(['_format' => $format_name]); + + $methods = $route->getMethods(); + $methods[] = 'POST'; + $route->setMethods($methods); $collection->add("$route_name.$method.$format_name", $format_route); } break; diff --git a/core/modules/rest/src/Plugin/rest/resource/EntityResource.php b/core/modules/rest/src/Plugin/rest/resource/EntityResource.php index 5d9849ded4..68abeea830 100644 --- a/core/modules/rest/src/Plugin/rest/resource/EntityResource.php +++ b/core/modules/rest/src/Plugin/rest/resource/EntityResource.php @@ -18,9 +18,11 @@ use Psr\Log\LoggerInterface; use Symfony\Component\DependencyInjection\ContainerInterface; use Drupal\rest\ModifiedResourceResponse; +use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException; use Symfony\Component\HttpKernel\Exception\BadRequestHttpException; +use Symfony\Component\HttpKernel\Exception\ConflictHttpException; use Symfony\Component\HttpKernel\Exception\HttpException; /** @@ -114,12 +116,19 @@ public static function create(ContainerInterface $container, array $configuratio * @param \Drupal\Core\Entity\EntityInterface $entity * The entity object. * + * @param \Symfony\Component\HttpFoundation\Request $request + * The HTTP request. + * * @return \Drupal\rest\ResourceResponse * The response containing the entity with its accessible fields. * * @throws \Symfony\Component\HttpKernel\Exception\HttpException */ - public function get(EntityInterface $entity) { + public function get(EntityInterface $entity, Request $request) { + if ($request->getMethod() === 'POST') { + throw new ConflictHttpException('POSTING to an already existing entity resource'); + } + $entity_access = $entity->access('view', NULL, TRUE); if (!$entity_access->isAllowed()) { throw new AccessDeniedHttpException($entity_access->getReason() ?: $this->generateFallbackAccessDeniedMessage($entity, 'view'));