comparison core/modules/jsonapi/src/Routing/EarlyFormatSetter.php @ 18:af1871eacc83

Update to Drupal core 8.7.1
author Chris Cannam
date Thu, 09 May 2019 15:33:08 +0100
parents
children
comparison
equal deleted inserted replaced
17:129ea1e6d783 18:af1871eacc83
1 <?php
2
3 namespace Drupal\jsonapi\Routing;
4
5 use Drupal\Core\Routing\RequestFormatRouteFilter;
6 use Symfony\Component\HttpFoundation\Request;
7 use Symfony\Component\Routing\RouteCollection;
8
9 /**
10 * Sets the 'api_json' format for requests to JSON:API resources.
11 *
12 * Because this module places all JSON:API resources at paths prefixed with
13 * /jsonapi, and therefore not shared with other formats,
14 * \Drupal\Core\Routing\RequestFormatRouteFilter does correctly set the request
15 * format for those requests. However, it does so after other filters, such as
16 * \Drupal\Core\Routing\ContentTypeHeaderMatcher, run. If those other filters
17 * throw exceptions, we'd like the error response to be in JSON:API format as
18 * well, so we set that format here, in a higher priority (earlier running)
19 * filter. This works so long as the resource format can be determined before
20 * running any other filters, which is the case for JSON:API resources per
21 * above.
22 *
23 * @internal
24 */
25 final class EarlyFormatSetter extends RequestFormatRouteFilter {
26
27 /**
28 * {@inheritdoc}
29 */
30 public function filter(RouteCollection $collection, Request $request) {
31 if (is_null($request->getRequestFormat(NULL))) {
32 $possible_formats = static::getAvailableFormats($collection);
33 if ($possible_formats === ['api_json']) {
34 $request->setRequestFormat('api_json');
35 }
36 }
37 return $collection;
38 }
39
40 }