How can run my code on user login Drupal 8


How can run my code on user login Drupal 8
By: Anonymous | Published On: Fri, 03/24/2023 - 15:50

In Drupal 8, you can execute code before user login by implementing a custom module and using a hook to execute the code. Here are the steps you can follow:

  1. Create a custom module by creating a directory in the modules directory of your Drupal installation. For example, if you want to name your module "custom_login", create a directory named "custom_login" in the modules directory.

  2. Create a custom_login.info.yml file in your module directory with the following contents:

name: Custom Login
type: module
description: Custom module for executing code before user login
core: 8.x
  1. Create a custom_login.module file in your module directory.

  2. In custom_login.module, implement the hook_user_login() function. This hook is called when a user logs in, before the user is redirected to their dashboard. Here is an example implementation of the hook:


 * Implements hook_user_login().
function custom_login_user_login($account) {
  // Add your code here to execute before user login.


  1. Save the custom_login.module file.

  2. Enable your custom module by going to the "Extend" page in Drupal's administrative interface and checking the box next to your module.

Now your code will execute before user login.

Need Help ?