Here's another approach.
In my example, I render the built-in user_profile_form()
, and simply unset the unnecessary fields. It's good because this way Drupal's own validation functions get called, the JavaScript-based password strength AND password matching indicator also gets rendered, and the field labels and descriptions are the same as on the user edit form (except that here I took out the e-mail changing text), but you can also change them, if you wish.
The result will look like this:
(Full screen)
This form will be visible at example.com/change-password
path (of course, example.com
should be substituted to your domain), and I'll also define a block for it.
/**
* Implements hook_menu().
*/
function YOURMODULENAME_menu() {
$items = array();
$items['change-password'] = array(
'title' => t('Change password'),
'description' => t('You can change your password here.'),
'page callback' => 'YOURMODULENAME_render_user_pass_change_form',
'access arguments' => array('access content'),
);
return $items;
}
/**
* Render the password changing form with the usage of Drupal's built-in user_profile_form
*
* @global type $user
* @return array The rendered form array for changing password
*/
function YOURMODULENAME_render_user_pass_change_form() {
global $user;
if (!user_is_logged_in()) {
drupal_access_denied();
}
module_load_include('inc', 'user', 'user.pages');
$form = drupal_get_form('user_profile_form', $user);
$request_new = l(t('Request new password'), 'user/password', array('attributes' => array('title' => t('Request new password via e-mail.'))));
$current_pass_description = t('Enter your current password to change the %pass. !request_new.', array('%pass' => t('Password'), '!request_new' => $request_new));
$form['account']['current_pass']['#description'] = $current_pass_description;
unset(
$form['account']['name'],
$form['account']['mail'],
$form['account']['status'],
$form['account']['roles'],
$form['locale'],
$form['l10n_client'],
$form['picture'],
$form['overlay_control'],
$form['contact'],
$form['timezone'],
$form['ckeditor'],
$form['metatags'],
$form['redirect']
);
return $form;
}
define('PASSWORD_CHANGING_BLOCK', 'password_changing_block');
/**
* Implements hook_block_info().
*/
function YOURMODULENAME_block_info() {
$blocks = array();
$blocks[PASSWORD_CHANGING_BLOCK] = array(
'info' => t('Block for changing password'), //The name that will appear in the block list.
'cache' => DRUPAL_CACHE_GLOBAL, // The block is the same for every user on every page where it is visible.
);
return $blocks;
}
/**
* Implements hook_block_view().
*
* Prepares the contents of the block.
*/
function YOURMODULENAME_block_view($delta = '') {
switch ($delta) {
case PASSWORD_CHANGING_BLOCK :
if(user_is_logged_in()){
$block['subject'] = t('Change Password');
$block['content'] = drupal_get_form('YOURMODULENAME_render_user_pass_change_form');
}
break;
}
return $block;
}
Of course, substitute YOURMODULENAME
with your own module's name (even near 'page callback'
and when calling drupal_get_form
)! You can also unset other fields if necessary (e.g. more fields are rendered via another module).
Clear the cache after putting it in your code.
After this, You can simply render this form by calling drupal_get_form('YOURMODULENAME_render_user_pass_change_form');
.