0

The following code works fine with Drupal 9.


namespace Drupal\vl_login\EventSubscriber;

use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
// use Symfony\Component\EventDispatcher\Event;
use Drupal\Component\EventDispatcher\Event;

/**
 * Event subscriber subscribing to KernelEvents::REQUEST.
 */
class RedirectAnonymousSubscriber implements EventSubscriberInterface {

  public function __construct() {
    $this->account = \Drupal::currentUser();
  }

  public function checkAuthStatus(GetResponseEvent $event) {

    $allowed = [
      'user.logout',
      'user.register',
      'user.login',
      'user.reset',
      'user.reset.form',
      'user.reset.login',
      'user.login.http',
      'user.logout.http',
      'user.pass'
    ];

    //redirect anonymous user to user login page
    if ( ($this->account->isAnonymous()) && ( !in_array(\Drupal::routeMatch()->getRouteName(), $allowed)) ) {
      $response = new RedirectResponse('/user/login', 302);
      $response->send();
    }
  }

  public static function getSubscribedEvents() {
    $events[KernelEvents::REQUEST][] = array('checkAuthStatus');
    return $events;
  }
}

The module file


use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
use Drupal\Core\Link;

/**
 * Implements hook_form_FORM_ID_alter().
 */
function vl_login_form_user_login_form_alter(&$form, FormStateInterface $form_state) {
  $form['#submit'][] = 'vl_login_user_login_submit';
}

/**
 * Form submission handler for user_login_form().
 *
 * Redirects the user to the dashboard after logging in.
 */
function vl_login_user_login_submit(&$form, FormStateInterface $form_state) {

  $url = Url::fromRoute('vl_login.dashboard');

  // Check if a destination was set, probably on an exception controller.
  // @see \Drupal\user\Form\UserLoginForm::submitForm()
  $request = \Drupal::service('request_stack')->getCurrentRequest();

  if (!$request->request->has('destination')) {
    $form_state->setRedirectUrl($url);
  }
  else {
    $request->query->set('destination', $request->request->get('destination'));
  }
}

I upgraded to Drupal 10 and I am not able to log in after I changed class from Symfony\Component\EventDispatcher\Event to Drupal\Component\EventDispatcher\Event because I get this exception.

TypeError: Drupal\vl_login\EventSubscriber\RedirectAnonymousSubscriber::checkAuthStatus(): Argument #1 ($event) must be of type Drupal\vl_login\EventSubscriber\GetResponseEvent

1
  • Next time include the full type error. The full error explains much.
    – mona lisa
    Aug 4, 2023 at 9:28

1 Answer 1

3

The correct Drupal 10 version would be using RequestEvent:

  public function checkAuthStatus(RequestEvent $event) {

    $allowed = [
      'user.logout',
      'user.register',
      'user.login',
      'user.reset',
      'user.reset.form',
      'user.reset.login',
      'user.login.http',
      'user.logout.http',
      'user.pass'
    ];

    //redirect anonymous user to user login page
    if ($this->account->isAnonymous() && !in_array(\Drupal::routeMatch()->getRouteName(), $allowed)) {
      $response = new RedirectResponse('/user/login', 302);
      $event->setResponse($response);
    }
  }

For all Drupal versions since Drupal 8, don't send a response, set it in the event.

And add the use statement at the top:

use Symfony\Component\HttpKernel\Event\RequestEvent;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.