0

I have a registration form, when submitted, the user gets logged in automatically.

I want to redirect the users to a specific page on login. Whether they are logged in using the login form or through the registration form.

I used the following code first,

function yourmodule_user_login($account) {
  // We want to redirect user on login.
  $response = new RedirectResponse("yourpath");
  $response->send();
  return;
}

This is not working because it's called before submit handlers so this would prevent them from being called.

Then I added a custom submit handler to the login form and redirect the users in that submit handler.

$form_state->setRedirect('mymodule.mypage');

This works fine only when the user logged in using the login form but not working for the registration form. When I use a custom submit handler for the registration and redirect the user there, I think at that time the user is not logged when the redirection occure.

Is there any other method to redirect the users on login through code?

3
  • 1
    There are at least a half dozen modules that do this for Drupal 8. Did you evaluate any?
    – Kevin
    Commented Dec 2, 2019 at 22:52
  • Possible duplicate of Redirect after user login
    – leymannx
    Commented Dec 3, 2019 at 18:41
  • 1
    Try @leymannx's answer... If this $form['#submit'][] = 'mymodule_user_login_submit'; doesn't work, try $form['actions']['submit']['#submit'][] = 'mymodule_user_login_submit';
    – STF
    Commented Dec 5, 2019 at 7:31

3 Answers 3

1

One way to do this without programming is to use Rules.

The advantage of using Rules for this is that if you want to modify the functionality - change the redirect page, put conditions on which users get redirected, etc. - those changes can be made with a few clicks rather than rewriting module code. This allows the site functionality to be modified and maintained without programming, much as Views and Workflow etc. all allow the site administrator to develop site functionality and content without direct programming. If you're building a site to hand off to a client, for instance, this is a very important point to take into account.

Rules automatically exposes core Drupal hooks as Symfony events and allows you to listen for these event and conditionally execute actions when those events happen. Redirecting on login is one of the classic use cases. Here is a Rule export from the rules_examples module - it is a configuration file so all you have to do is import it and it will work. You can edit it through the Rules UI or manually to suit your specific situation.

langcode: en
status: false
dependencies: {  }
id: redirect_on_login
label: 'Redirect users on Login'
events:
  -
    event_name: rules_user_login
description: 'Issue #2858817-8: Redirect users to /special-page on login.'
tags:
  - login
  - redirect
  - rules_examples
config_version: '3'
expression:
  id: rules_rule
  uuid: 7263942c-3847-460d-af3f-6c78f80fbcc1
  conditions:
    id: rules_and
    uuid: a8c1bb95-b783-4238-a7e1-9b4b9fb96ca4
    conditions: {  }
  actions:
    id: rules_action_set
    uuid: d17285ef-4de4-498a-8b78-654cf79566ee
    actions:
      -
        id: rules_action
        uuid: a529b090-e6f3-4421-aede-75489267d888
        context_values:
          url: /special-page
        context_mapping: {  }
        context_processors:
          url:
            rules_tokens: {  }
        provides_mapping: {  }
        action_id: rules_page_redirect
2
  • There are two rules modules. One is business rules. Which module are you talking about? Rules or business rules?
    – Ahmad
    Commented Dec 3, 2019 at 8:30
  • And there is no custom way other than rules?
    – Ahmad
    Commented Dec 3, 2019 at 8:30
0

Subscribe to the UserLogin event like below:

in yourmodule/src/Event/UserLoginEvent:

<?php

namespace Drupal\yourmodule\Event;

use Drupal\user\UserInterface;
use Symfony\Component\EventDispatcher\Event;

/**
 * Event that is fired when a user logs in.
 */
class UserLoginEvent extends Event {

  const EVENT_NAME = 'yourmodule_user_login';

  /**
   * The user account.
   *
   * @var \Drupal\user\UserInterface
   */
  public $account;

  /**
   * Constructs the object.
   *
   * @param \Drupal\user\UserInterface $account
   *   The account of the user logged in.
   */
  public function __construct(UserInterface $account) {
    $this->account = $account;
  }

}

in yourmodule/src/UserLoginSubscriber.php:

<?php

namespace Drupal\yourmodule\EventSubscriber;

use Drupal\yourmodule\Event\UserLoginEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

/**
 * Class UserLoginSubscriber.
 *
 * @package Drupal\yourmodule\EventSubscriber
 */
class UserLoginSubscriber implements EventSubscriberInterface {

  /**
   * {@inheritdoc}
   */
  public static function getSubscribedEvents() {
    return [
      // Static class constant => method on this class.
      UserLoginEvent::EVENT_NAME => 'onUserLogin',
    ];
  }

  /**
   * React to the user login event dispatched.
   *
   * @param \Drupal\yourmodule\Event\UserLoginEvent $event
   *   Dat event object yo.
   */
  public function onUserLogin(UserLoginEvent $event) {
    $response = new RedirectResponse("yourpath");
    $response->send();
    return;
  }

}
1
  • 2
    It doesn't look like you're generating the event anywhere in the above code? That's the tricky part. If for example you generate the event in hook_user_login() then you will run into the same problem as in the original post - the redirect will happen before the submit handlers are run.
    – anonymous
    Commented Dec 2, 2019 at 21:16
0

include this code at the top

use Symfony\Component\HttpFoundation\RedirectResponse;

then code this for redirection

$response = new RedirectResponse('YOURPATH');
$response->send();
return;
1
  • I already did that and mentioned in the question.
    – Ahmad
    Commented Dec 4, 2019 at 4:52

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.