core/modules/rest/src/RequestHandler.php | 27 +++++++++++---------------- 1 file changed, 11 insertions(+), 16 deletions(-) diff --git a/core/modules/rest/src/RequestHandler.php b/core/modules/rest/src/RequestHandler.php index ca8318e..aea0509 100644 --- a/core/modules/rest/src/RequestHandler.php +++ b/core/modules/rest/src/RequestHandler.php @@ -162,35 +162,30 @@ protected function getArgumentResolver(RouteMatchInterface $route_match, $unseri $route_arguments = $route_match->getParameters()->all(); $upcasted_route_arguments = $route_arguments; - // \Drupal\rest\Plugin\ResourceInterface plugins historically receive the - // unserialized request body as the N+1th method argument, where N is the - // number of route parameters specified on the accompanying route. To be - // able to use the argument resolver, which is not based on position but on - // name and typehint, specify commonly used names here. 0 or 1 of these will - // be used. - if (isset($unserialized)) { + // For request methods that have request bodies, ResourceInterface plugin + // methods historically receive the unserialized request body as the N+1th + // method argument, where N is the number of route parameters specified on + // the accompanying route. To be able to use the argument resolver, which is + // not based on position but on name and typehint, specify commonly used + // names here. Similarly, those methods receive the original stored object + // as the first method argument. + if (in_array($request->getMethod(), ['PATCH', 'POST'], TRUE)) { $upcasted_route_arguments['entity'] = $unserialized; $upcasted_route_arguments['data'] = $unserialized; $upcasted_route_arguments['unserialized'] = $unserialized; - } - // In the case of GET the entity is stored in $upcasted_route_arguments in - // the $entity_type_id key, not as 'entity', which - // \Drupal\rest\Plugin\rest\resource\EntityResource::get() expects as their name. - if (!in_array($request->getMethod(), ['PATCH', 'POST'], TRUE) && empty($upcasted_route_arguments['entity'])) { // Try to find a parameter which is an entity. foreach ($route_arguments as $value) { if ($value instanceof EntityInterface) { - $upcasted_route_arguments['entity'] = $value; + $upcasted_route_arguments['original_entity'] = $value; } } } - - if (in_array($request->getMethod(), ['PATCH', 'POST'], TRUE) && empty($upcasted_route_arguments['original_entity'])) { + else { // Try to find a parameter which is an entity. foreach ($route_arguments as $value) { if ($value instanceof EntityInterface) { - $upcasted_route_arguments['original_entity'] = $value; + $upcasted_route_arguments['entity'] = $value; } } }