function RequestFormatRouteFilter::filter

Same name in other branches
  1. 9 core/lib/Drupal/Core/Routing/RequestFormatRouteFilter.php \Drupal\Core\Routing\RequestFormatRouteFilter::filter()
  2. 8.9.x core/lib/Drupal/Core/Routing/RequestFormatRouteFilter.php \Drupal\Core\Routing\RequestFormatRouteFilter::filter()
  3. 11.x core/lib/Drupal/Core/Routing/RequestFormatRouteFilter.php \Drupal\Core\Routing\RequestFormatRouteFilter::filter()

Overrides FilterInterface::filter

1 method overrides RequestFormatRouteFilter::filter()
EarlyFormatSetter::filter in core/modules/jsonapi/src/Routing/EarlyFormatSetter.php
Filters the route collection against a request.

File

core/lib/Drupal/Core/Routing/RequestFormatRouteFilter.php, line 19

Class

RequestFormatRouteFilter
Provides a route filter, which filters by the request format.

Namespace

Drupal\Core\Routing

Code

public function filter(RouteCollection $collection, Request $request) {
    // Determine the request format.
    $default_format = static::getDefaultFormat($collection);
    // If the request does not specify a format then use the default.
    if (is_null($request->getRequestFormat(NULL))) {
        $format = $default_format;
        $request->setRequestFormat($default_format);
    }
    else {
        $format = $request->getRequestFormat($default_format);
    }
    $routes_with_requirement = [];
    $routes_without_requirement = [];
    $result_collection = new RouteCollection();
    
    /** @var \Symfony\Component\Routing\Route $route */
    foreach ($collection as $name => $route) {
        if (!$route->hasRequirement('_format')) {
            $routes_without_requirement[$name] = $route;
            continue;
        }
        else {
            $routes_with_requirement[$name] = $route;
        }
    }
    foreach ($routes_with_requirement as $name => $route) {
        // If the route has no _format specification, we move it to the end. If it
        // does, then no match means the route is removed entirely.
        if (($supported_formats = array_filter(explode('|', $route->getRequirement('_format')))) && in_array($format, $supported_formats, TRUE)) {
            $result_collection->add($name, $route);
        }
    }
    foreach ($routes_without_requirement as $name => $route) {
        $result_collection->add($name, $route);
    }
    if (count($result_collection)) {
        return $result_collection;
    }
    // We do not throw a
    // \Symfony\Component\Routing\Exception\ResourceNotFoundException here
    // because we don't want to return a 404 status code, but rather a 406.
    $available_formats = static::getAvailableFormats($collection);
    $not_acceptable = new NotAcceptableHttpException("No route found for the specified format {$format}. Supported formats: " . implode(', ', $available_formats) . '.');
    if ($available_formats) {
        $links = [];
        foreach ($available_formats as $available_format) {
            $url = Url::fromUri($request->getUri(), [
                'query' => [
                    '_format' => $available_format,
                ],
            ])
                ->toString(TRUE)
                ->getGeneratedUrl();
            $content_type = $request->getMimeType($available_format);
            $links[] = "<{$url}>; rel=\"alternate\"; type=\"{$content_type}\"";
        }
        $not_acceptable->setHeaders([
            'Link' => implode(', ', $links),
        ]);
    }
    throw $not_acceptable;
}

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