drupal 7 cusomized用户配置文件模板无法保存更改

时间:2014-02-22 19:24:36

标签: drupal-7 drupal-theming drupal-templates

我想自定义用户个人资料编辑页面。编辑路径为:xxx / user / 2 / edit 1.在我的templage文件中:

     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;
}

在我的user-profile-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']);?>

现在表单看起来像预期的那样,但是,当我更改密码时,没有错误,但我无法使用新密码登录。我再次测试了它,似乎只有管理员可以更改它,身份验证用户无法更改自己的密码。

1 个答案:

答案 0 :(得分: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'));
}