You are here

class UserRouteAlterSubscriber in Drupal 8

Same name and namespace in other branches
  1. 9 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() {
    $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)) {
        $formats = explode('|', $route
          ->getRequirement('_format'));
        $formats = array_unique(array_merge($formats, $this->serializerFormats));
        $route
          ->setRequirement('_format', implode('|', $formats));
      }
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
UserRouteAlterSubscriber::$serializerFormats protected property The available serialization formats.
UserRouteAlterSubscriber::getSubscribedEvents public static function Returns an array of event names this subscriber wants to listen to.
UserRouteAlterSubscriber::onRoutingAlterAddFormats public function Adds supported formats to the user authentication HTTP routes.
UserRouteAlterSubscriber::__construct public function UserRouteAlterSubscriber constructor.