diff --git a/core/modules/rest/src/RequestHandler.php b/core/modules/rest/src/RequestHandler.php index 54d9d8737f..eebcdb29cc 100644 --- a/core/modules/rest/src/RequestHandler.php +++ b/core/modules/rest/src/RequestHandler.php @@ -5,6 +5,7 @@ use Drupal\Component\Utility\ArgumentsResolver; use Drupal\Core\Cache\CacheableResponseInterface; use Drupal\Core\DependencyInjection\ContainerInjectionInterface; +use Drupal\Core\Entity\EntityInterface; use Drupal\Core\Entity\EntityStorageInterface; use Drupal\Core\Routing\RouteMatchInterface; use Symfony\Component\DependencyInjection\ContainerAwareInterface; @@ -158,10 +159,37 @@ protected function getArgumentResolver(RouteMatchInterface $route_match, $unseri // to the raw arguments. $raw_route_arguments = $route_match->getRawParameters()->all() + $route->getDefaults(); - $upcasted_route_arguments = $route_match->getParameters()->all(); - $upcasted_route_arguments['entity'] = $unserialized; - $upcasted_route_arguments['data'] = $unserialized; - $upcasted_route_arguments['unserialized'] = $unserialized; + $route_arguments = $route_match->getParameters()->all(); + $upcasted_route_arguments = $route_arguments; + + // Just set the values, if they are actually defined, otherwise, the + // argument resolver believe + if (isset($unserialized)) { + $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 expects as their name. + if (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; + } + } + } + + if (empty($upcasted_route_arguments['original_entity'])) { + // Try to find a parameter which is an entity. + foreach ($route_arguments as $value) { + if ($value instanceof EntityInterface) { + $upcasted_route_arguments['original_entity'] = $value; + } + } + } // Parameters which are not defined on the route object, but still are // essential for access checking are passed as wildcards to the argument