2

I would like to change labels only on user edit page. String overrides module would not work because it would rename all labels (user register and user edit). I tried the following:

function theme_preprocess_page(&$vars, $hook) {
  if ($vars['template_files'][2] == 'page-user-edit') {
    $vars['content'] = str_replace('Password', 'New Password', $vars['content']);
    $vars['content'] = str_replace('Confirm password', 'Confirm new password', $vars['content']);
  }
}

6 Answers 6

8

If you're willing to do it in a module (you mentioned String Overrides module) you can easily do it with a simple hook_form_alter() in a small custom module.

function mymodule_form_user_edit_form_alter($form, &$form_state) {
  $form['desired_element']['#label'] = t('My label');
  return $form;
}

This method uses hook_form_FORM_ID_alter(). This way you'll be able to set individual labels as you see fit, though.

4
  • form_alter didn't work for me because form doesn't expose label Commented Sep 5, 2011 at 16:25
  • The form should definitely allow access to the #label property as shown above. try printing out the array ($form) so you can see everything contained in it. Commented Sep 6, 2011 at 20:12
  • I printed form to a log with watchdog, bud I did not find #label there Commented Sep 6, 2011 at 20:27
  • You definitely cannot edit the password label using just hook_form_alter(). See other answers below for the correct hooks to use. Commented Mar 3, 2017 at 22:55
5

The password inputs are in fact a separate form element (password_confirm -- Form API reference) and although it is themable (see theme_password_confirm), all you can influence is the description, prefix/postfix and element title.

The real work happens in expand_password_confirm, which in turn is a process function specified in system_elements. The latter is an implementation of hook_elements and it lets Drupal know how to handle various form elements.

Luckily, one can use hook_elements() to specify custom form elements and that is the one you can use to override the system processing for the password_confirm element.

A sample implementation follows:

<?php
/**
 * Implementation of hook_elements()
 * @see http://api.drupal.org/api/drupal/developer--hooks--core.php/function/hook_elements/6
 */
function mymodule_elements() {
  $type['password_confirm']['#process'][] = 'mymodule_process_password_confirm';
  return $type;
}

/**
 * Do a bit of processing for the password_confirm element.
 * Original processing happens in expand_password_confirm()
 */
function mymodule_process_password_confirm($element) {
  // check if we the parent element is 'account'. On the user edit page, that is the case
  // Of course, one could use arg() to check the path too
  if ($element['#array_parents'][0] == 'account') {
    $element['pass1']['#title'] = 'New password';
    $element['pass2']['#title'] = 'Confirm new password';
  }

  return $element;
}

Please note this is based on Drupal 6. In Drupal 7, hook_elements() became hook_element_info because there is a proper alter hook, hook_element_info_alter

1
  • 1
    Actually, I managed to get it changed in MYTHEME_password_confirm, all I had to do was change $element['pass1']['#title'] and then re-render the elements. See my answer below. Commented Apr 3, 2012 at 14:13
1

Put the below code in your theme folder's template.php file. Just double check that your template.php already has an existing YOURTHEMENAME_preprocess_page(&$vars) function, in that case, only add what is inside the function below.

function YOURTHEMENAME_form_user_profile_form_alter(&$form, &$form_state, $form_id){

 $form['account']['current_pass']['#title'] = 'New Label';

}

function YOURTHEMENAME_preprocess_page(&$vars){

$vars['page']['content']['system_main']['account']['pass']['pass1']['#title'] = 'Some New Label for Confirm Password'; 

$vars['page']['content']['system_main']['account']['pass']['pass2']['#title'] = 'Some Other  Label Confirm Password';
}
1
  • doesnot work for me in drupal 8 Commented Oct 22, 2019 at 7:51
1

This is a solution for Drupal 6, based on theme_password_confirm:

function MYTHEME_password_confirm($element) {
  foreach (array('pass1', 'pass2') as $part) {
    $element[$part]['#attributes']['placeholder'] = $element[$part]['#title']; // Set the title text as placeholder
    unset($element[$part]['#title']); // Remove the title.
  }
  $element['#children'] = ''; // #children holds the html output
  foreach (element_children($element) as $key) {
    unset($element[$key]['#printed']); // Prepare to re-render.
    $element['#children'] .= drupal_render($element[$key]);
  }
  return theme('form_element', $element, $element['#children']);
}
1

Working solution for Drupal 7:

function MYMODULE_form_alter(&$form, &$form_state, $form_id) {
    if($form_id == 'user_profile_form'){
      // Here we need to provide an extra #process handler to allow us to modify
      // the password element that FAPI expands.
      $element_info = element_info('password_confirm');
      $process = $element_info['#process'];
      $process[] = 'MYMODULE_process_password_confirm';
      $form['account']['pass']['#process'] =  $process;
    }
}
  function MYMODULE_process_password_confirm($element) {
    $element['pass1']['#title'] = t("New Password");
    $element['pass2']['#title'] = t("Confirm New Password");
    return $element;
  }
0

Try this one, hope it will work.

function your_module_form_alter(&$form, &$form_state, $form_id) {

  if($form_id == 'user_register_form') {

   $form['account']['pass']['#process'] = array('form_process_password_confirm',  'register_alter_password_confirm');

}


function register_alter_password_confirm($element) {

   $element['pass1']['#title'] = t("Password:");

   $element['pass2']['#title'] = t("Confirm Password:");

   return $element;

}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.