Drupal 7 drupal 7自定义用户配置文件模板无法保存更改
我想自定义我的用户配置文件编辑页面。编辑路径为:xxx/user/2/edit 1.在我的模板文件中:Drupal 7 drupal 7自定义用户配置文件模板无法保存更改,drupal-7,drupal-theming,drupal-templates,Drupal 7,Drupal Theming,Drupal Templates,我想自定义我的用户配置文件编辑页面。编辑路径为:xxx/user/2/edit 1.在我的模板文件中: 函数MYTHEME\u主题(){ “用户配置文件格式”=>数组( 'arguments'=>array('form'=>NULL), '呈现元素'=>'形式', “模板”=>“用户配置文件表单”, 'path'=>drupal\u get\u path('theme','bootstrap\u subtheme')。/templates', ), } 函数MYTHEME_预处理_用户_配置文件
函数MYTHEME\u主题(){
“用户配置文件格式”=>数组(
'arguments'=>array('form'=>NULL),
'呈现元素'=>'形式',
“模板”=>“用户配置文件表单”,
'path'=>drupal\u get\u path('theme','bootstrap\u subtheme')。/templates',
),
}
函数MYTHEME_预处理_用户_配置文件_表单(&$vars){
drupal_set_title(“帐户设置”);
未设置($vars['form']['account']['mail']['description']);
$vars['form']['account']['mail']['title']=t('Email:');
未设置($vars['form']['picture']['title']);
未设置($vars['form']['picture']['picture\u delete']);
$vars['form']['picture']['picture#u upload']['title']=t('upload new photo:');
$vars['form']['picture']['picture_upload']['attributes']['class'][]='formtext';
未设置($vars['form']['picture']['picture_upload']['description']);
未设置($vars['form']['account']['current_pass']['description']);
未设置($vars['form']['account']['pass']['description']);
未设置($vars['form']['account']['pass2']['#description']);
未设置($vars['form']['field_birth']['und']['#prefix']);
未设置($vars['form']['field_birth']['und']['后缀']);
未设置($vars['form']['field_birth']['und']['title']);
$vars['form']['account']['pass']['pass1']['#title']=t('新密码:');
$vars['form']['account']['pass']['pass2']['#title']=t('重新输入新密码:');
$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;
}
在我的user-profile-form.tpl.php文件中:
性别:
密码:
现在表单看起来和预期的一样,但是,当我更改密码时,没有错误,但我无法使用新密码登录。我再次测试了它,似乎只有管理员可以更改它,身份验证用户不能更改自己的密码。我已经创建了自定义用户“帐户”-这样的页面。我的方式并不能完全回答你的问题,但我认为这是更好的方式来完成你想要的,并给你更多的灵活性 在我的自定义模块中,我有这些挂钩。(用您自己的模块名称替换模块) 这将禁用默认的用户表单路径
function module_admin_paths_alter(&$paths) {
$paths['user'] = FALSE;
$paths['user/*'] = FALSE;
}
创建自定义用户管理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;
}
用户表单和页面。这包括一些在此示例中未处理的额外字段:
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;
}
表单验证:
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);
}
}
提交表格。。精简版:
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'));
}
谢谢,这个方法很好用。但是渲染(drupal_get_form('module_edit_account_form',$user));不起作用。我认为我们不能将用户对象传递给这个函数。我已将用户对象移动到提交函数中。是的,在我自己的代码中,我将自定义用户数据传递给drupal\u get\u表单函数。。这只是一个例子。。