function UserRouteAlterSubscriber::onRoutingAlterAddFormats

Same name in other branches
  1. 9 core/modules/serialization/src/EventSubscriber/UserRouteAlterSubscriber.php \Drupal\serialization\EventSubscriber\UserRouteAlterSubscriber::onRoutingAlterAddFormats()
  2. 8.9.x core/modules/serialization/src/EventSubscriber/UserRouteAlterSubscriber.php \Drupal\serialization\EventSubscriber\UserRouteAlterSubscriber::onRoutingAlterAddFormats()
  3. 11.x core/modules/serialization/src/EventSubscriber/UserRouteAlterSubscriber.php \Drupal\serialization\EventSubscriber\UserRouteAlterSubscriber::onRoutingAlterAddFormats()

Adds supported formats to the user authentication HTTP routes.

Parameters

\Drupal\Core\Routing\RouteBuildEvent $event: The event to process.

File

core/modules/serialization/src/EventSubscriber/UserRouteAlterSubscriber.php, line 45

Class

UserRouteAlterSubscriber
Alters user authentication routes to support additional serialization formats.

Namespace

Drupal\serialization\EventSubscriber

Code

public function onRoutingAlterAddFormats(RouteBuildEvent $event) {
    $route_names = [
        'user.login_status.http',
        'user.login.http',
        'user.logout.http',
        'user.pass.http',
    ];
    $routes = $event->getRouteCollection();
    foreach ($route_names as $route_name) {
        if (($route = $routes->get($route_name)) && $route->hasRequirement('_format')) {
            $formats = explode('|', $route->getRequirement('_format'));
            $formats = array_unique(array_merge($formats, $this->serializerFormats));
            $route->setRequirement('_format', implode('|', $formats));
        }
    }
}

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