Adding Reset Password Support To Drupal 6 Password Recovery Email
Note: This post is over two years old and so the information contained here might be out of date. If you do spot something please leave a comment and we will endeavour to correct.
3rd January 2012 - 4 minutes read time
Drupal is capable of sending out a few different emails to users depending on different actions. The emails can be customised quite nicely with usernames, passwords, email addresses and other things by using a set of tokens. The password recovery email states that you can use the "!password" token to send the user their new password, but after a few tests I found that this token doesn't get replaced when the email is sent out.
The simplest solution here is just to remove this token from the description for this email. However, if you do want to allow user's to reset (and receive) their passwords by using this form then there are a couple of simple things you can do.
The first thing to be done is alter the user_pass form so that it uses a custom submit function that we will write. Add the following form hook to a module (or create your own for this purpose).
/**
* Implements hook_form_form-id_alter().
*
* Overwrite the submit function of the request new password form.
*/
function mymodule_form_user_pass_alter(&$form, &$form_state) {
$form['#submit'] = array('mymodule_user_pass_submit');
}
The next step is to rewrite the code of the user_pass_submit() submit function, but to create a save the user's password. All we need to do is call user_password() to create a password, save it to the user's account using user_save() and then make sure that it is passed to the email template.
/**
* Overrides the submit function user_pass_submit().
*
* @see mymodule_form_user_pass_alter()
*/
function mymodule_user_pass_submit($form, &$form_state) {
global $language;
$account = $form_state['values']['account'];
// Generate new password
$password = user_password();
// Save new password
$savedetails = array('pass' => $password);
user_save($account, $savedetails);
// Set password parameter of $account object so that the new password is emailed.
$account->password = $password;
// Continue with the rest of the submit function
// Mail one time login URL and instructions using current language.
_user_mail_notify('password_reset', $account, $language);
watchdog('user', 'Password reset instructions mailed to %name at %email.', array('%name' => $account->name, '%email' => $account->mail));
drupal_set_message(t('Further instructions have been sent to your e-mail address.'));
$form_state['redirect'] = 'user';
return;
}
Now when your user's request a new password there current password will be changed and emailed to them.
Hi,
Little bit out of topic.
Do you have a snippets to redirect user to homepage"" instead of "/user",
after user request to change password from "user/password"
You could try using the Login Destination to redirect the user, although that might not be what you are after.
An alternative would be to use a hook_form_alter() on the password form to add a redirect to the form submission.
Drupal's path system gives authors the ability to override the path of an item of content. Whilst the internal path of a page might be "/node/1" the external path can be anything you want, and can be set when editing the item of content.
I've been doing a bit of Drupal migration recently, and one of the tasks I have undertaken is to migrate "likes" from a Drupal 7 site to a Drupal 10 site.
Context definitions in Drupal are really useful when you want to inject context into your plugins and I have been using them for a little while now. It has made building custom blocks to add content to pages much easier as I don't need to copy the same block of code to find an entity of a particular sort into the block plugin.
I previously looked at injecting context providers into context aware plugins. This time I will explore more about creating our own context providers to plug into this system.
Comments
Exactelly what i'm searching, thanks
Submitted by Anonymous on Tue, 01/10/2012 - 11:05
PermalinkSubmitted by Xavier on Tue, 05/08/2012 - 09:23
PermalinkSubmitted by philipnorton42 on Tue, 05/08/2012 - 10:39
PermalinkAdd new comment