How to add “Remember me” checkbox to login form in Drupal 8, 9, 10

  • Home
  • Drupal
  • How to add “Remember me” checkbox to login form in Drupal 8, 9, 10

Question

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:

  1. 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”.
  2. Create a file called “remember_me.info.yml” in the “modules/custom/remember_me” folder of your Drupal installation.
  3. Create a file called “remember_me.module” in the “modules/custom/remember_me” folder of your Drupal installation.
  4. 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:

				
					<?php

/**
 * Implements hook_form_alter().
 */
function remember_me_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id) {
    if ($form_id == 'user_login_form') {
        $form['remember_me'] = [
            '#type'          => 'checkbox',
            '#title'         => t('Remember me'),
            '#default_value' => FALSE,
        ];
    }
}

				
			

    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.

Screenshot

Leave A Comment

Cart
No products in the cart.
X