class UserRouteAlterSubscriber

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

Alters user authentication routes to support additional serialization formats.

Hierarchy

  • class \Drupal\serialization\EventSubscriber\UserRouteAlterSubscriber implements \Symfony\Component\EventDispatcher\EventSubscriberInterface

Expanded class hierarchy of UserRouteAlterSubscriber

1 string reference to 'UserRouteAlterSubscriber'
serialization.services.yml in core/modules/serialization/serialization.services.yml
core/modules/serialization/serialization.services.yml
1 service uses UserRouteAlterSubscriber
serialization.user_route_alter_subscriber in core/modules/serialization/serialization.services.yml
Drupal\serialization\EventSubscriber\UserRouteAlterSubscriber

File

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

Namespace

Drupal\serialization\EventSubscriber
View source
class UserRouteAlterSubscriber implements EventSubscriberInterface {
    
    /**
     * The available serialization formats.
     *
     * @var array
     */
    protected $serializerFormats = [];
    
    /**
     * UserRouteAlterSubscriber constructor.
     *
     * @param array $serializer_formats
     *   The available serializer formats.
     */
    public function __construct(array $serializer_formats) {
        $this->serializerFormats = $serializer_formats;
    }
    
    /**
     * {@inheritdoc}
     */
    public static function getSubscribedEvents() : array {
        $events[RoutingEvents::ALTER][] = 'onRoutingAlterAddFormats';
        return $events;
    }
    
    /**
     * Adds supported formats to the user authentication HTTP routes.
     *
     * @param \Drupal\Core\Routing\RouteBuildEvent $event
     *   The event to process.
     */
    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));
            }
        }
    }

}

Members

Title Sort descending Modifiers Object type Summary
UserRouteAlterSubscriber::$serializerFormats protected property The available serialization formats.
UserRouteAlterSubscriber::getSubscribedEvents public static function
UserRouteAlterSubscriber::onRoutingAlterAddFormats public function Adds supported formats to the user authentication HTTP routes.
UserRouteAlterSubscriber::__construct public function UserRouteAlterSubscriber constructor.

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