From 9f0c39dbb72f94882dc2a4236e1915f44f01b655 Mon Sep 17 00:00:00 2001 From: Claudiu Cristea Date: Tue, 4 Jun 2019 17:10:19 +0300 Subject: [PATCH] Issue #83: Make /user/register route aware of the new config. --- oe_authentication.services.yml | 3 ++- src/Routing/RouteSubscriber.php | 33 +++++++++++++++++++++++++++------ 2 files changed, 29 insertions(+), 7 deletions(-) diff --git a/oe_authentication.services.yml b/oe_authentication.services.yml index a003bb96..731e7d8c 100644 --- a/oe_authentication.services.yml +++ b/oe_authentication.services.yml @@ -5,7 +5,8 @@ services: tags: - { name: access_check, applies_to: _external_user_access_check } oe_authentication.route_subscriber: - class: \Drupal\oe_authentication\Routing\RouteSubscriber + class: Drupal\oe_authentication\Routing\RouteSubscriber + arguments: ['@config.factory'] tags: - { name: event_subscriber } oe_authentication.event_subscriber: diff --git a/src/Routing/RouteSubscriber.php b/src/Routing/RouteSubscriber.php index e36c5e5b..efaba90d 100644 --- a/src/Routing/RouteSubscriber.php +++ b/src/Routing/RouteSubscriber.php @@ -4,6 +4,7 @@ namespace Drupal\oe_authentication\Routing; +use Drupal\Core\Config\ConfigFactoryInterface; use Drupal\Core\Routing\RouteSubscriberBase; use Symfony\Component\Routing\Route; use Symfony\Component\Routing\RouteCollection; @@ -13,6 +14,23 @@ */ class RouteSubscriber extends RouteSubscriberBase { + /** + * The config factory service. + * + * @var \Drupal\Core\Config\ConfigFactoryInterface + */ + protected $configFactory; + + /** + * Constructs a new route event subscriber. + * + * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory + * The config factory service. + */ + public function __construct(ConfigFactoryInterface $config_factory) { + $this->configFactory = $config_factory; + } + /** * {@inheritdoc} */ @@ -33,12 +51,15 @@ protected function alterRoutes(RouteCollection $collection): void { } // Replace the core register route controller. - $route = $collection->get('user.register'); - if ($route instanceof Route) { - $defaults = $route->getDefaults(); - unset($defaults['_form']); - $defaults['_controller'] = '\Drupal\oe_authentication\Controller\RegisterController::register'; - $route->setDefaults($defaults); + $config = $this->configFactory->get('oe_authentication.settings'); + if ($config->get('redirect_user_register_route')) { + $route = $collection->get('user.register'); + if ($route instanceof Route) { + $defaults = $route->getDefaults(); + unset($defaults['_form']); + $defaults['_controller'] = '\Drupal\oe_authentication\Controller\RegisterController::register'; + $route->setDefaults($defaults); + } } // Replace the cas callback route controller.