Skip to content

Commit

Permalink
Issue #83: Listen to ConfgEvents::SAVE to detect changes instead of f…
Browse files Browse the repository at this point in the history
…orm API.
  • Loading branch information
claudiu-cristea committed Jun 4, 2019
1 parent ec18135 commit 8e49cd8
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 8 deletions.
5 changes: 5 additions & 0 deletions oe_authentication.services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,8 @@ services:
tags:
- { name: event_subscriber }
arguments: ['@config.factory', '@messenger']
oe_authentication.config_subscriber:
class: Drupal\oe_authentication\Event\UserRegisterRouteRedirectConfigSubscriber
arguments: ['@router.builder']
tags:
- { name: event_subscriber }
57 changes: 57 additions & 0 deletions src/Event/UserRegisterRouteRedirectConfigSubscriber.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

declare(strict_types = 1);

namespace Drupal\oe_authentication\Event;

use Drupal\Core\Config\ConfigCrudEvent;
use Drupal\Core\Config\ConfigEvents;
use Drupal\Core\Routing\RouteBuilderInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

/**
* Acts when oe_authentication.settings:redirect_user_register_route changes.
*/
class UserRegisterRouteRedirectConfigSubscriber implements EventSubscriberInterface {

/**
* The route builder service.
*
* @var \Drupal\Core\Routing\RouteBuilderInterface
*/
protected $routeBuilder;

/**
* Constructs a new event subscriber instance.
*
* @param \Drupal\Core\Routing\RouteBuilderInterface $route_builder
* The route builder service.
*/
public function __construct(RouteBuilderInterface $route_builder) {
$this->routeBuilder = $route_builder;
}

/**
* {@inheritdoc}
*/
public static function getSubscribedEvents(): array {
return [
ConfigEvents::SAVE => 'onConfigSave',
];
}

/**
* Acts when oe_authentication.settings:redirect_user_register_route changes.
*
* @param \Drupal\Core\Config\ConfigCrudEvent $event
* The config CRUD event.
*/
public function onConfigSave(ConfigCrudEvent $event): void {
if ($event->getConfig()->getName() === 'oe_authentication.settings') {
if ($event->isChanged('redirect_user_register_route')) {
$this->routeBuilder->rebuild();
}
}
}

}
8 changes: 0 additions & 8 deletions src/Form/AuthenticationSettingsForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,6 @@ public function buildForm(array $form, FormStateInterface $form_state) {
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
$original_redirect_user_register_route = $this->config(static::CONFIG_NAME)
->get('redirect_user_register_route');

$this->config(static::CONFIG_NAME)
->set('protocol', $form_state->getValue('protocol'))
->set('register_path', $form_state->getValue('register_path'))
Expand All @@ -110,11 +107,6 @@ public function submitForm(array &$form, FormStateInterface $form_state) {
->set('redirect_user_register_route', $form_state->getValue('redirect_user_register_route'))
->save();
parent::submitForm($form, $form_state);

// Rebuild the routes if the redirect user register config has changed.
if ($original_redirect_user_register_route != $form_state->getValue('redirect_user_register_route')) {
$this->routeBuilder->rebuild();
}
}

/**
Expand Down

0 comments on commit 8e49cd8

Please sign in to comment.