问题
I want to custom my user profile edit page. The edit path is: xxx/user/2/edit 1. In my templage file:
function MYTHEME_theme() {
'user_profile_form' => array(
'arguments' => array('form' => NULL),
'render element' => 'form',
'template' => 'user-profile-form',
'path' => drupal_get_path('theme', 'bootstrap_subtheme').'/templates',
),
}
function MYTHEME_preprocess_user_profile_form(&$vars) {
drupal_set_title('Account settings');
unset($vars['form']['account']['mail']['#description']);
$vars['form']['account']['mail']['#title'] = t('Email:');
unset($vars['form']['picture']['#title']);
unset($vars['form']['picture']['picture_delete']);
$vars['form']['picture']['picture_upload']['#title'] = t('Upload new photo:');
$vars['form']['picture']['picture_upload']['#attributes']['class'][] = 'form-text';
unset($vars['form']['picture']['picture_upload']['#description']);
unset($vars['form']['account']['current_pass']['#description']);
unset($vars['form']['account']['pass']['#description']);
unset($vars['form']['account']['pass2']['#description']);
unset($vars['form']['field_birthday']['und']['#prefix']);
unset($vars['form']['field_birthday']['und']['#suffix']);
unset($vars['form']['field_birthday']['und']['#title']);
$vars['form']['account']['pass']['pass1']['#title'] = t('New password:');
$vars['form']['account']['pass']['pass2']['#title'] = t('Re-enter new password:');
$vars['form']['actions']['submit']['#value'] = t('Save changes');
$vars['form']['actions']['submit']['#attributes']['class'][] = 'btn';
$vars['form']['actions']['submit']['#attributes']['class'][] = 'btn-purple';
$vars['form']['account']['mail']['#size']=20;
}
in my user-profile-form.tpl.php file:
<div class="row-fluid">
<div class="span1 offset2">Gender:</div>
<div class="span4 offset1"><?php print render($form['field_gender']);?></div>
</div>
<div class="row-fluid">
<div class="span1 offset2">Password:</div>
<div class="span4 offset1"><?php print render($form['account']['pass']);?></div>
</div>
<?php print render($form['actions']);?>
Now the form looks as expected, however, when i change password, there is no error, but i can not login with the new password. I tested it again, it seems that only administer can change it, the authentication user can not change their own password.
回答1:
I have created custom user "account" -page like this. My way is not exactly answer to your question but i think this is better way to accomplish what you looking for and gives you more flexibility.
In my custom module i have these hooks. (replace module with your own module name).
This disables default user form paths.
function module_admin_paths_alter(&$paths) {
$paths['user'] = FALSE;
$paths['user/*'] = FALSE;
}
Create your custom user manage url..
function module_menu() {
$items = array();
// User account url
$items['manage/account'] = array(
'title' => 'User account',
'description' => 'Edit account settings',
'page callback' => 'module_edit_account',
'access callback' => 'user_is_logged_in',
'type' => MENU_NORMAL_ITEM,
);
return $items;
}
User form and page. This includes some extra fields that is not handled in this example:
function module_edit_account() {
drupal_set_title(t('Account'));
global $user;
$html = render(drupal_get_form('module_edit_account_form', $user));
return $html;
}
function module_edit_account_form($form, &$form_state, $user_data) {
// If no values.. use userdata..
if (@empty($form_state['values'])) {
$values = $user_data;
} else {
$values = $form_state['values'];
}
$form = array();
// We dont want to deal with hierarchical form values.
$form['#tree'] = false;
$form['user'] = array(
'#type' => 'fieldset',
'#title' => t('User information'),
'#collapsible' => FALSE,
'#collapsed' => FALSE,
'#weight' => 2,
);
$langs = language_list();
$lang_options = array();
foreach ($langs as $iso => $lang) {
$lang_options[$iso] = $lang->name;
}
$form['user']['firstname'] = array(
'#type' => 'textfield',
'#title' => t('Firstname'),
'#description' => t('Define firstname.'),
'#default_value' => (@!empty($values['firstname']) ? $values['firstname'] : ''),
'#required' => false,
);
$form['user']['lastname'] = array(
'#type' => 'textfield',
'#title' => t('Lastname'),
'#description' => t('Define lastname.'),
'#default_value' => (@!empty($values['lastname']) ? $values['lastname'] : ''),
'#required' => false,
);
$form['user']['pass'] = array(
'#type' => 'password_confirm',
'#description' => t('If you want to change your current password type new password.'),
'#required' => false,
);
$form['user']['langcode'] = array(
'#type' => 'select',
'#title' => t('Language'),
'#options' => $lang_options,
'#default_value' => (@!empty($values['language']) ? $values['language'] : ''),
'#required' => true
);
$form['user']['mail'] = array(
'#type' => 'textfield',
'#title' => t('Email'),
'#description' => t('Define email address.'),
'#default_value' => (@!empty($values['mail']) ? $values['mail'] : ''),
'#required' => true,
);
$form['user']['phone'] = array(
'#type' => 'textfield',
'#title' => t('Phone number'),
'#description' => t('Define telephone number.'),
'#default_value' => (@!empty($values['phone']) ? $values['phone'] : ''),
'#required' => false,
);
$form['user']['address'] = array(
'#type' => 'textfield',
'#title' => t('Address'),
'#description' => t('Define address.'),
'#default_value' => (@!empty($values['address']) ? $values['address'] : ''),
'#required' => false,
);
$form['user']['postcode'] = array(
'#type' => 'textfield',
'#title' => t('Postcode'),
'#description' => t('Define postcode.'),
'#default_value' => (@!empty($values['postcode']) ? $values['postcode'] : ''),
'#required' => false,
);
$form['user']['city'] = array(
'#type' => 'textfield',
'#title' => t('City'),
'#description' => t('Define city.'),
'#default_value' => (@!empty($values['city']) ? $values['city'] : ''),
'#required' => false,
);
$form['btn_submit'] = array(
'#type' => 'submit',
'#attributes' => array('class' => array('button')),
'#value' => t('Save changes'),
'#submit' => array('module_account_form_submit'),
'#validate' => array('module_account_form_validate'),
);
return $form;
}
Form validation:
function module_account_form_validate($form, &$form_state) {
// Check user email..
if ($error = user_validate_mail($form_state['values']['mail'])) {
form_set_error('mail', $error);
}
}
Form submit.. stripped version:
function module_account_form_submit($form, &$form_state) {
global $user;
$values = $form_state['values'];
// Get user info. Use user global for security reasons and not id from form.
$user_account = user_load($user->uid);
$user_edit = array(
'mail' => $values['mail'],
'language' => $values['langcode']
);
// If user want to change password..
if (@!empty($values['pass'])) {
$user_edit['pass'] = $values['pass'];
}
// Get default timezone from system
$user_account->timezone = date_default_timezone(false);
// Save existing user
user_save($user_account, $user_edit);
drupal_set_message(t('Settings saved'));
}
来源:https://stackoverflow.com/questions/21959283/drupal-7-cusomized-user-profile-template-can-not-save-changes