in this article, I'll show you how to redirect user after login in drupal 8 & 9.
Create redirect_user_after_login.info.yml file
name: Redirect User After Login
description: customize url redirect after user logged in successfully.
package: Custom
type: module
core: 8.x
core_version_requirement: ^8 || ^9
configure: redirect_user_after_login.settings
then enable this module:
Create redirect_user_after_login.links.menu.yml file
redirect_user_after_login.admin:
title: 'Login Redirect'
parent: system.admin_config_system
route_name: redirect_user_after_login.settings
Create redirect_user_after_login.routing.yml file
redirect_user_after_login.settings:
path: '/admin/config/system/login_redirect'
defaults:
_form: '\Drupal\redirect_user_after_login\Form\RedirectUserAfterLoginForm'
_title: 'Set Login Redirection Url'
requirements:
_permission: 'access content'
Create src/Form/RedirectUserAfterLoginForm.php file
<?php
namespace Drupal\redirect_user_after_login\Form;
use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;
class RedirectUserAfterLoginForm extends ConfigFormBase
{
/**
* {@inheritdoc}
*/
public function getFormId()
{
return 'redirect_user_after_login_form';
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state)
{
// Form constructor.
$form = parent::buildForm($form, $form_state);
// Default settings.
$config = $this->config('redirect_user_after_login.settings');
$form['url_after_login'] = array(
'#type' => 'textfield',
'#title' => $this->t('Url Redirection'),
'#required' => TRUE,
'#default_value' => $config->get('redirect_user_after_login.path'),
'#description' => $this->t('Add a valid url or <front> for front page'),
);
return $form;
}
/**
* {@inheritdoc}
*/
public function validateForm(array &$form, FormStateInterface $form_state) {
$path = $form_state->getValue('url_after_login');
$is_valid = \Drupal::service('path.validator')->isValid($path);
if ($is_valid == NULL) {
$form_state->setErrorByName('url_after_login', $this->t('Path not found.'));
}
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
$config = $this->config('redirect_user_after_login.settings');
$config->set('redirect_user_after_login.path', $form_state->getValue('url_after_login'));
$config->save();
return parent::submitForm($form, $form_state);
}
/**
* Gets the configuration names that will be editable.
*
* @return array
* An array of configuration object names that are editable if called in
* conjunction with the trait's config() method.
*/
protected function getEditableConfigNames()
{
return [
'redirect_user_after_login.settings',
];
}
}
then you can set your login redirection url
Create redirect_user_after_login.module file
To redirect user after login in drupal 8 & 9, You need to alter the login form and add a submit handler which will take care of the redirection using RedirectResponse.
<?php
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
use Symfony\Component\HttpFoundation\RedirectResponse;
/**
* Implements hook_form_alter().
*/
function redirect_user_after_login_form_alter(&$form, FormStateInterface $form_state, $form_id)
{
switch ($form_id) {
case 'user_login_form':
$form['#submit'][] = '_login_page_redirect_user_login_form_submit';
break;
}
}
/**
* Custom submit handler for login form.
*/
function _login_page_redirect_user_login_form_submit($form, FormStateInterface $form_state)
{
$path = \Drupal::config('redirect_user_after_login.settings')->get('redirect_user_after_login.path');
if ($path == '<front>') {
$path = '/';
}
$homeResponse = new RedirectResponse(URL::fromUserInput($path)->toString());
$homeResponse->send();
}
Next steps
- Clear your Drupal 8 caches. To do this I use this Drush command:
drush cr
if you don’t currently use Drush, I highly recommend using it, or the Drupal Console. - I hope you found this article useful. let me know if you have any questions and I’ll be happy to answer them.