UserRouteAlterSubscriber.php in Drupal 9
File
core/modules/serialization/src/EventSubscriber/UserRouteAlterSubscriber.php
View source
<?php
namespace Drupal\serialization\EventSubscriber;
use Drupal\Core\Routing\RouteBuildEvent;
use Drupal\Core\Routing\RoutingEvents;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class UserRouteAlterSubscriber implements EventSubscriberInterface {
protected $serializerFormats = [];
public function __construct(array $serializer_formats) {
$this->serializerFormats = $serializer_formats;
}
public static function getSubscribedEvents() {
$events[RoutingEvents::ALTER][] = 'onRoutingAlterAddFormats';
return $events;
}
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)) {
$formats = explode('|', $route
->getRequirement('_format'));
$formats = array_unique(array_merge($formats, $this->serializerFormats));
$route
->setRequirement('_format', implode('|', $formats));
}
}
}
}