function RequestHandler::createArgumentResolver

Same name in other branches
  1. 9 core/modules/rest/src/RequestHandler.php \Drupal\rest\RequestHandler::createArgumentResolver()
  2. 8.9.x core/modules/rest/src/RequestHandler.php \Drupal\rest\RequestHandler::createArgumentResolver()
  3. 10 core/modules/rest/src/RequestHandler.php \Drupal\rest\RequestHandler::createArgumentResolver()

Creates an argument resolver, containing all REST parameters.

Parameters

\Drupal\Core\Routing\RouteMatchInterface $route_match: The route match.

mixed $unserialized: The unserialized data.

\Symfony\Component\HttpFoundation\Request $request: The request.

Return value

\Drupal\Component\Utility\ArgumentsResolver An instance of the argument resolver containing information like the 'entity' we process and the 'unserialized' content from the request body.

1 call to RequestHandler::createArgumentResolver()
RequestHandler::delegateToRestResourcePlugin in core/modules/rest/src/RequestHandler.php
Delegates an incoming request to the appropriate REST resource plugin.

File

core/modules/rest/src/RequestHandler.php, line 236

Class

RequestHandler
Acts as intermediate request forwarder for resource plugins.

Namespace

Drupal\rest

Code

protected function createArgumentResolver(RouteMatchInterface $route_match, $unserialized, Request $request) {
    $route = $route_match->getRouteObject();
    // Defaults for the parameters defined on the route object need to be added
    // to the raw arguments.
    $raw_route_arguments = $route_match->getRawParameters()
        ->all() + $route->getDefaults();
    $route_arguments = $route_match->getParameters()
        ->all();
    $upcasted_route_arguments = $route_arguments;
    // 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 type hint, specify commonly used
    // names here. Similarly, those methods receive the original stored object
    // as the first method argument.
    $route_arguments_entity = NULL;
    // Try to find a parameter which is an entity.
    foreach ($route_arguments as $value) {
        if ($value instanceof EntityInterface) {
            $route_arguments_entity = $value;
            break;
        }
    }
    if (in_array($request->getMethod(), [
        'PATCH',
        'POST',
    ], TRUE)) {
        if (is_object($unserialized)) {
            $upcasted_route_arguments['entity'] = $unserialized;
            $upcasted_route_arguments['data'] = $unserialized;
            $upcasted_route_arguments['unserialized'] = $unserialized;
        }
        else {
            $raw_route_arguments['data'] = $unserialized;
            $raw_route_arguments['unserialized'] = $unserialized;
        }
        $upcasted_route_arguments['original_entity'] = $route_arguments_entity;
    }
    else {
        $upcasted_route_arguments['entity'] = $route_arguments_entity;
    }
    // Parameters which are not defined on the route object, but still are
    // essential for access checking are passed as wildcards to the argument
    // resolver.
    $wildcard_arguments = [
        $route,
        $route_match,
    ];
    $wildcard_arguments[] = $request;
    if (isset($unserialized)) {
        $wildcard_arguments[] = $unserialized;
    }
    return new ArgumentsResolver($raw_route_arguments, $upcasted_route_arguments, $wildcard_arguments);
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.