How to add “Remember me” checkbox to login form in Drupal 8, 9 and 10?
Solution
To add a “Remember me” checkbox to a login form in Drupal, you can follow these steps:
Create a custom module (if you don’t have one already) or use an existing custom module to add this functionality. Let’s assume the module is called “remember_me”.
Create a file called “remember_me.info.yml” in the “modules/custom/remember_me” folder of your Drupal installation.
Create a file called “remember_me.module” in the “modules/custom/remember_me” folder of your Drupal installation.
Open the “remember_me.info.yml” file and add the following code:
name: 'Remember me'
type: module
description: 'This module enables the Remember me checkbox on the login page.'
core_version_requirement: ^8 || ^9 || ^10
package: 'custom'
version: 1.0
5. Open the “remember_me.module” file and add the following code:
6. Save the file. 7. Clear the Drupal cache to ensure that the module is recognized. You can do this by visiting the Performance page (/admin/config/development/performance) in your Drupal admin dashboard and clicking the “Clear all caches” button. 8. Now, when you visit the login form on your Drupal site, you should see a “Remember me” checkbox added to the form.
Note : When the checkbox is checked and the user logs in, the “Remember me” value will be stored in a cookie. If the checkbox is unchecked, the cookie will be deleted. Please note that modifying Drupal core files directly is not recommended, so it’s best to use a custom module to add this functionality.