3 namespace Drupal\serialization\EventSubscriber;
5 use Drupal\Core\Routing\RouteBuildEvent;
6 use Drupal\Core\Routing\RoutingEvents;
7 use Symfony\Component\EventDispatcher\EventSubscriberInterface;
10 * Alters user authentication routes to support additional serialization formats.
12 class UserRouteAlterSubscriber implements EventSubscriberInterface {
15 * The available serialization formats.
19 protected $serializerFormats = [];
22 * UserRouteAlterSubscriber constructor.
24 * @param array $serializer_formats
25 * The available serializer formats.
27 public function __construct(array $serializer_formats) {
28 $this->serializerFormats = $serializer_formats;
34 public static function getSubscribedEvents() {
35 $events[RoutingEvents::ALTER][] = 'onRoutingAlterAddFormats';
40 * Adds supported formats to the user authentication HTTP routes.
42 * @param \Drupal\Core\Routing\RouteBuildEvent $event
43 * The event to process.
45 public function onRoutingAlterAddFormats(RouteBuildEvent $event) {
47 'user.login_status.http',
52 $routes = $event->getRouteCollection();
53 foreach ($route_names as $route_name) {
54 if ($route = $routes->get($route_name)) {
55 $formats = explode('|', $route->getRequirement('_format'));
56 $formats = array_unique(array_merge($formats, $this->serializerFormats));
57 $route->setRequirement('_format', implode('|', $formats));