Score:-1

How do I port this event subscriber?

cn flag

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

id flag
Next time include the full type error. The full error explains much.
Score:1
cn flag

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;
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.