final class EarlyFormatSetter in Drupal 9
Same name and namespace in other branches
- 8 core/modules/jsonapi/src/Routing/EarlyFormatSetter.php \Drupal\jsonapi\Routing\EarlyFormatSetter
- 10 core/modules/jsonapi/src/Routing/EarlyFormatSetter.php \Drupal\jsonapi\Routing\EarlyFormatSetter
Sets the 'api_json' format for requests to JSON:API resources.
Because this module places all JSON:API resources at paths prefixed with /jsonapi, and therefore not shared with other formats, \Drupal\Core\Routing\RequestFormatRouteFilter does correctly set the request format for those requests. However, it does so after other filters, such as \Drupal\Core\Routing\ContentTypeHeaderMatcher, run. If those other filters throw exceptions, we'd like the error response to be in JSON:API format as well, so we set that format here, in a higher priority (earlier running) filter. This works so long as the resource format can be determined before running any other filters, which is the case for JSON:API resources per above.
@internal
Hierarchy
- class \Drupal\Core\Routing\RequestFormatRouteFilter implements FilterInterface
- class \Drupal\jsonapi\Routing\EarlyFormatSetter
Expanded class hierarchy of EarlyFormatSetter
1 string reference to 'EarlyFormatSetter'
- jsonapi.services.yml in core/
modules/ jsonapi/ jsonapi.services.yml - core/modules/jsonapi/jsonapi.services.yml
1 service uses EarlyFormatSetter
- jsonapi.route_filter.format_setter in core/
modules/ jsonapi/ jsonapi.services.yml - Drupal\jsonapi\Routing\EarlyFormatSetter
File
- core/
modules/ jsonapi/ src/ Routing/ EarlyFormatSetter.php, line 25
Namespace
Drupal\jsonapi\RoutingView source
final class EarlyFormatSetter extends RequestFormatRouteFilter {
/**
* {@inheritdoc}
*/
public function filter(RouteCollection $collection, Request $request) {
if (is_null($request
->getRequestFormat(NULL))) {
$possible_formats = static::getAvailableFormats($collection);
if ($possible_formats === [
'api_json',
]) {
$request
->setRequestFormat('api_json');
}
}
return $collection;
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
EarlyFormatSetter:: |
public | function |
Filters the route collection against a request and returns all matching
routes. Overrides RequestFormatRouteFilter:: |
|
RequestFormatRouteFilter:: |
protected static | function | Gets the set of formats across all routes in the collection. | |
RequestFormatRouteFilter:: |
protected static | function | Determines the default request format. |