修改用户当前密码流程(自定义form表单练习)
function mytest_menu() { $items = array(); $items['user/%user/password'] = array( 'title' => '修改密码', 'page callback' => 'drupal_get_form', 'page arguments' => array('change_user_pass',1), 'access callback' => 'user_edit_access', 'access arguments' => array(1), 'type' => MENU_LOCAL_TASK, ); return $items;} function change_user_pass($form, &$form_state, $account) { $form['current_pass'] = array( '#type' => 'password', '#title' => t('当前密码'), // '#prefix' => '', '#maxlength' => 64, '#size' => 30, '#required' => TRUE, ); $form['pass'] = array( '#type' => 'password_confirm', ); $form['submit'] = array( '#type' => 'submit', '#value' => '提交', '#validate' => array('change_user_pass_validate'), '#submit' => array('change_user_pass_submit'), ); $form['#validate'][] = 'user_validate_current_pass'; return $form;}function change_user_pass_validate($form, &$form_state) { if (!empty($form_state['values']['current_pass'])) { # code... $_form_current_pass = $form_state['values']['current_pass']; $_hash_form_current_pass = module_invoke('user', 'hash_password', $_form_current_pass); if ($_hash_form_current_pass != $user->pass) { form_set_error('current_pass', t("Your current password is missing or incorrect; it's required to change the Password.")); } }}function change_user_pass_submit($form, &$form_state) { global $user; $new_user = null; $new_user = array( 'name' => $user->name, 'uid' => $user->uid, 'pass' => rtrim($form_state['values']['pass']), 'status '=>$user->status , ); $new_user['roles']=$user->roles; user_save($user,$new_user); // $form_state['redirect'] = ''; drupal_set_message(t('Password success!'));}
?