Community Documentation

Customize a 'Password Reset' form

Last updated July 11, 2008. Created by raspberryman on July 2, 2007.
Edited by add1sun. Log in to edit this page.

Here is a method of customizing a 'password reset' page and hiding the account settings until the password is reset.

Problem:

I wanted the user to just see a 'Password Reset' form, and not the full account edit form, when they reset their password or login via the url in the welcome email. I also want this form to stay there until they do reset their password.

Solution:

1) When a user resets their password (Or clicks on the URL in their registration email), the form values for their user/#/edit page are empty. When they login normally, this is not the case. Therefore, an empty $edit variable on user login is our flag:

<?php
function mymodule_user($op, &$edit, &$account, $category = NULL) {

  if (
$op == 'login') {
     if (empty(
$edit)) {
        
// Add a temporary variable to the user object called 'reset'
       
user_save($account, array('reset' => TRUE));
     }
     break;
  }   
}
?>

2) Now you can customize the 'Password Reset' form:

<?php
function mymodule_form_alter($form_id, &$form) {

  if (
'user_edit' == $form_id) {

    if (isset(
$form['_account']['#value']->reset) ) {

     
$form['account']['#title'] = t('Password Reset');
     
// Include mail for the user.module validation code
     
$form['account']['mail'] = array('#type' => 'value', '#value' => $form['_account']['#value']->mail);
     
$form['picture'] = NULL;
     
$form['theme_select'] = NULL;
     
$form['comment_settings'] = NULL;
     
$form['timezone'] = NULL;

    }
  }
}
?>

3) Lastly, be sure to remove the special 'reset' variable we added to the user. I do this in user 'submit', but you can even do it in hook_form_alter(), depending on your needs.

<?php
function mymodule_user($op, &$edit, &$account, $category = NULL) {

  if (
$op == 'submit') {
    if (
$account->reset) {
     
user_save($account, array('reset' => NULL));
      return;
    }
  }
}
?>

About this page

Drupal version
Drupal 5.x

Reference

Drupal’s online documentation is © 2000-2012 by the individual contributors and can be used in accordance with the Creative Commons License, Attribution-ShareAlike 2.0. PHP code is distributed under the GNU General Public License.
nobody click here