Hook_user_login Drupal 8

Posted onby admin

Drupal 8 - News - Help - Tutorial - And more. Main navigation. To make a redirection after user login, we can use the hook hookuserlogin. Note: You cannot. Introduction to YAML in Drupal 8. YAML is a data serialisation format that is both powerful and easy for us humans to read and understand. In Drupal 8 it's used where Drupal needs a.

  1. Hook_user_login Drupal 8 Free
  2. Drupal 8 Hook_user_login Redirect
  3. Drupal 8 Release
  4. Hook_user_login Drupal 8 Download

When using the one-time login link userpassreset calls drupalgoto after userloginfinalize has invoked hookuserlogin, so this is not an issue. Pfrenssen's comment is weboide commented 21 March 2012 at 19:24.

  • In drupal 8 I have two separate roles admin and manager. If user logins with manager username and password, should be redirected to different page. We can use module but I need to do it through custom code in drupal 8. Please help me in the code to perform this condition in drupal 8 custom module.
  • Use IMCE files browser for custom field on Drupal 8/9 Drupal 9 add inline JavaScript to a page programmatically Private Tempstore - Store data on Drupal 9 session.

Drupal 8 is about Object-Oriented Programming (OOP) and a firm positioning of Drupal in the enterprise. The introduction of key Symfony components was meant to herald new beginnings and enforce a more professional programming paradigm.


The hook system which is as old as the project allows Drupal core to call certain functions defined in modules at specific places. In other words, Drupal allows developers to interact with core code when some things happen in the system, e.g. when a user logs in or out, a node is about to be saved, after it has been inserted in the database, updated, or deleted, etc.

Drupal 7 Implementation

When an event occurs and it is identified by a string which is the hook, e.g. user_login, node_insert, etc., Drupal calls any functions named according to the pattern of MODULE_HOOK with arguments to the function that have been specified by the module that introduces the hook. For a module called custom_hooks_demo, the function will be as follows:

What is possible here, is only limited by the developer's imagination. You have all values submitted from the login form and the user object.

Some hooks are alterable by appending 'alter' to the function names like with e.g. hook_TYPE_alter(), where TYPE may be form, links, image_styles, date_formats, system_info, node_grants, node_view, block_info, etc. These functions are handed over to drupal_alter(), in order to make sure that all alter operations are carried out consistently.

Contributed modules are allowed to introduce their own hooks or alter hooks. For example, implementing Views hooks allows you to modify a lot of the functionality of this immensely popular module. Then, if I want to have a random display of views' results, I may implement hook_views_pre_render as follows:

There are a couple of other views hooks that can be implemented to achieve the same result. This is a credit to the flexibility of both the views module and the hook system.

Finally, custom modules too can define their own hooks so that other developers can react to pre-determined events in the custom module.

Drupal 7 Invocation

A hook may be called in the following ways:

1 module_invoke_all($hook):

All enabled modules that implement the hook are discovered, and the hook functions called one after the other. $hook is the name of the hook.

2 module_invoke($module, $hook):

A hook in a given module is called where $hook is the name of the hook and $module is the machine name of the module, e.g.:

3 In certain cases, the hook function has to be called directly, e.g. if arguments need to be passed by reference. Here is an example from the user module:

Then the function is called as follows:

Sometimes, you need to find all modules that implement a hook, before carrying out an action on each of them, e.g. to run all cron jobs.

Drupal 8 Implementation

The EventDispatcher Component is the Symfony way of allowing components to interact with each other by registering, dispatching, and listening to events. This serves the same purpose as the hook system.

Drupal 8 does not only listen to Symfony-defined events, but it comes with its own events, which contributed and custom modules can listen for.

However, the hook system remains intact but with an OOP approach. Hooks available in Drupal 8 core may fall into the following categories:

1 Unchanged

Many hooks common in Drupal 7 are more or less the same, e.g. hook_user_login, hook_theme, hook_form_FORM_ID_alter, with some slight changes, mostly due to the architectural move to OOP. For example:

2 Removed

Some hooks have been completely removed, either because they are no longer required, or there are new ways of achieving the same result. For example hook_watchdog, hook_boot, hook_init, and hook_exit.

3 Modified

Hook_user_login Drupal 8 Free

With the introduction of @EntityType annotation as part of the new Plugin system, hook_entity_info is no longer used to define entity types. However, this hook was replaced with hook_entity_type_build, to add information to existing entity types. Entity definitions can be changed with hook_entity_type_alter.

4 Added

Several new hooks have been added, particularly related to entities. In Drupal 7, there are 12 hook_entity_* hooks, while in Drupal 8, there are 43 hook_entity_* and 19 hook_ENTITY_TYPE_* ones.

Drupal 8 Invocation

This is where there is a divergence from Drupal 7. The core/lib/Drupal/Core/Extension/ModuleHandler.php class manages modules and it is provided as the module_handler service.

There are two ways of getting a service.

a. To accommodate legacy procedural code, there is a service container wrapper class corelibDrupal.php, which can be called statically. The moduleHandler() method returns the module_handler service.

Then you can do:

These calls are also chainable, so you can write the above as:


Let us implement the same hook_user_login example above in Drupal 8.

Before this can work, you need to import the RedirectResponse class right at the top of the file, just after the opening PHP tag, e.g.

This is how to invoke all implementations of a hook in a module file, e.g. to run all cron jobs, just like in Drupal 7 above.

b. The recommended way in classes is through dependency injection. Your class needs to implement the ContainerInjectionInterface and you inject the ModuleHandlerInterface into your class constructor. Then you get an instance of ModuleHandler from the module_handler service in the service container. This will allow you to do something like the following:

Here is an example of a Controller class where we inject ModuleHandlerInterface and LoggerInterface through the constructor method. In the customRunAllCrons() method, we get all modules that implement hook_cron, run, and log each one.


The hook system remains largely unchanged in Drupal 8. However, you need to be aware of any changes to particular hooks, e.g. the number and order of arguments required. Make sure to check on those that have been either completely removed or just renamed. Above all, check out the new ones, especially for interacting with entities and entity types.

Ideally, if you identify any events you can subscribe to, the EventDispatcher system is the way forward when interacting with other components of your application.


The demo module for this article is in the X-Team Drupal 8 Examples Github repository.

Drupal 8 Hook_user_login Redirect

Recommended reading

Have you ever been asked to log into a website while you are viewing a page? And after doing so you get redirected to some page other than the one you were reading? This is an obvious and rather common usability problem. When this happens people lose track of what they were doing and some might not even bother to go back. Let's find out how to solve this in Drupal 8.

In a recent project a client wisely requested exactly that: whenever a user logs into the site, redirect them to the page they were before clicking the login link. This seemed like a very common request so we looked for a contrib module that provided the functionality. Login Destination used to do it in Drupal 7. Sadly the Drupal 8 version of this module does not provide the functionality yet.

Other modules, and some combinations of them, were tested without success. Therefore, we built Login Return Page. It a very small module that just does one thing and it does it well: it appends destination=/current/page to all the links pointing to /user/login effectively redirecting users to the page they were viewing before login. The project is waiting to be approved before promoting it to full project.

Drupal 8 Release

Have you had a similar need? Are there other things you are requested to do after login? Please share them in the comments.

Hook_user_login Drupal 8 Download

UPDATE: It seems to this is a regression from Drupal 7 and there is an issue that would fix it. Thanks to Wim Leers for letting me know about it.