2014-02-22 65 views
0

我想定製我的用戶配置文件編輯頁面。編輯路徑是:XXX /用戶/ 2 /編輯 1.我templage文件:drupal 7 cusomized用戶配置文件模板無法保存更改

 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; 
} 
我的用戶配置文件,form.tpl.php文件

<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']);?> 

現在的形式但是,當我更改密碼時,沒有錯誤,但我無法用新密碼登錄。我再次測試它,似乎只有管理員可以更改它,認證用戶不能更改自己的密碼。

回答

0

我已經創建了像這樣的自定義用戶「帳戶」 - 頁面。我的方式並不完全回答你的問題,但我認爲這是更好的方式來完成你所尋找的,並給你更多的靈活性。

在我的自定義模塊中,我有這些掛鉤。 (用你自己的模塊名稱替換模塊)。

這會禁用默認的用戶表單路徑。

function module_admin_paths_alter(&$paths) { 

    $paths['user'] = FALSE; 
    $paths['user/*'] = FALSE; 

} 

創建自定義的用戶管理網址..

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')); 
} 
+0

感謝,這種方法效果很好。但渲染(drupal_get_form('module_edit_account_form',$ user));不起作用。我認爲我們不能將用戶對象傳遞給這個函數。我已將用戶對象移至提交功能。 – user3210341

+0

是的,在我自己的代碼中,我將自定義用戶數據傳遞給drupal_get_form函數..所以這只是一個例子.. – Hardy