Mercurial > hg > isophonics-drupal-site
comparison core/modules/serialization/src/EventSubscriber/UserRouteAlterSubscriber.php @ 0:4c8ae668cc8c
Initial import (non-working)
author | Chris Cannam |
---|---|
date | Wed, 29 Nov 2017 16:09:58 +0000 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:4c8ae668cc8c |
---|---|
1 <?php | |
2 | |
3 namespace Drupal\serialization\EventSubscriber; | |
4 | |
5 use Drupal\Core\Routing\RouteBuildEvent; | |
6 use Drupal\Core\Routing\RoutingEvents; | |
7 use Symfony\Component\EventDispatcher\EventSubscriberInterface; | |
8 | |
9 /** | |
10 * Alters user authentication routes to support additional serialization formats. | |
11 */ | |
12 class UserRouteAlterSubscriber implements EventSubscriberInterface { | |
13 | |
14 /** | |
15 * The available serialization formats. | |
16 * | |
17 * @var array | |
18 */ | |
19 protected $serializerFormats = []; | |
20 | |
21 /** | |
22 * UserRouteAlterSubscriber constructor. | |
23 * | |
24 * @param array $serializer_formats | |
25 * The available serializer formats. | |
26 */ | |
27 public function __construct(array $serializer_formats) { | |
28 $this->serializerFormats = $serializer_formats; | |
29 } | |
30 | |
31 /** | |
32 * {@inheritdoc} | |
33 */ | |
34 public static function getSubscribedEvents() { | |
35 $events[RoutingEvents::ALTER][] = 'onRoutingAlterAddFormats'; | |
36 return $events; | |
37 } | |
38 | |
39 /** | |
40 * Adds supported formats to the user authentication HTTP routes. | |
41 * | |
42 * @param \Drupal\Core\Routing\RouteBuildEvent $event | |
43 * The event to process. | |
44 */ | |
45 public function onRoutingAlterAddFormats(RouteBuildEvent $event) { | |
46 $route_names = [ | |
47 'user.login_status.http', | |
48 'user.login.http', | |
49 'user.logout.http', | |
50 'user.pass.http', | |
51 ]; | |
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)); | |
58 } | |
59 } | |
60 } | |
61 | |
62 } |