Answers:
我现在还找不到任何东西,这听起来很方便,所以这里有一个小模块,提供了一个带有更改密码形式的块:
文件:change_password.info
name = Change Password
description = Provides a block containing a form for the current user to change their password.
core = 7.x
文件:change_password.module
<?php
/**
* Implements hook_block_info().
*/
function change_password_block_info() {
return array(
'change_password_form' => array(
'info' => t('Change Password Form')
)
);
}
/**
* Implements hook_block_view().
*/
function change_password_block_view($delta = '') {
$block = array();
// Only show the block for a logged-in user.
if ($delta == 'change_password_form' && user_is_logged_in()) {
$block['subject'] = t('Change Password');
$block['content'] = drupal_get_form('change_password_form');
}
return $block;
}
/**
* Password change form.
*/
function change_password_form($form, &$form_state) {
// Sanity check
if (user_is_anonymous()) {
return $form; // Or drupal_access_denied()?
}
// Get the currently logged in user object.
$form['#account'] = $GLOBALS['user'];
// Textfield cor current password confirmation.
$form['current_pass'] = array(
'#type' => 'password',
'#title' => t('Current password'),
'#size' => 25,
'#required' => TRUE
);
// Password confirm field.
$form['account']['pass'] = array(
'#type' => 'password_confirm',
'#size' => 25,
'#title' => t('New Password'),
'#required' => TRUE
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Submit')
);
return $form;
}
/**
* Validate handler for change_password_form().
*/
function change_password_form_validate(&$form, &$form_state) {
// Make sure the password functions are present.
require_once DRUPAL_ROOT . '/' . variable_get('password_inc', 'includes/password.inc');
// Make sure the provided current password is valid for this account.
if (!user_check_password($form_state['values']['current_pass'], $form['#account'])) {
form_set_error('current_pass', t('The current password you provided is incorrect.'));
}
}
/**
* Submit handler for change_password_form().
*/
function change_password_form_submit(&$form, &$form_state) {
// Set up the edit array to pass to user_save()
$edit = array('pass' => $form_state['values']['pass']);
// Save the account with the new password.
user_save($form['#account'], $edit);
// Inform the user.
drupal_set_message(t('Your password has been changed.'));
}
它(经过了轻微的测试)可以正常工作,但是您可能想要一次让自己安心使用。
更新我已经把它作为一个沙箱项目,以防万一有人感兴趣。
这是另一种方法。
在我的示例中,我渲染了内置的user_profile_form()
,并简单地取消设置了不必要的字段。很好,因为这样可以调用Drupal自己的验证函数,还可以呈现基于JavaScript的密码强度和密码匹配指示器,并且字段标签和描述与用户编辑表单上的相同(除了这里我取出了e -mail更改文本),但也可以根据需要进行更改。
结果将如下所示:
(全屏)
这种形式将在example.com/change-password
路径上可见(当然,example.com
应替换为您的域),并且我还将为其定义一个块。
/**
* 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;
}
当然,请YOURMODULENAME
用您自己的模块名称替换(甚至'page callback'
在调用时drupal_get_form
)。您也可以根据需要取消设置其他字段(例如,通过其他模块渲染更多字段)。
将缓存放入代码后,将其清除。
之后,您只需调用即可呈现此表单drupal_get_form('YOURMODULENAME_render_user_pass_change_form');
。