1

How to change the form label from Password to New Password and Confirm password to New Confirm password on THEME_form_user_form_alter(without changing any twig files)? I had tried kint($form['account']['pass']) to see any possible way, no clue...

Current Change Password Form

Current password

|||||||||||||||||

Password (to New Password)

|||||||||||||||||

Confirm password (to New Confirm password)

|||||||||||||||||

function THEME_form_user_form_alter(&$form, $form_state) {
  kint($form['account']);
}

There are no #title element on pass field.

enter image description here

  • looks to me that current_pass must be coming from a custom or contrib module, as mine doesn't have that. – No Sssweat Jan 30 at 9:40
  • @NoSssweat, how about 'pass', I would like to change the label to "New Password" – kpda806G Jan 30 at 9:43
  • 1
    These #titles are defined in /core/lib/Drupal/Core/Render/Element/PasswordConfirm.php. It looks complicated to change, you might have to just do it via javascript/jQuery. – No Sssweat Jan 30 at 9:46
1

You have to create a new module and use hook_element_info_alter to alter title input like this:

function my_module_element_info_alter(array &$types) {
  if (isset($types['password_confirm'])) {
    $types['password_confirm']['#process'][] = '_mymodule_change_title';
  }
}

and process function

function _mymodule_change_title(&$element) {
  $element['pass1']['#title'] = t('New title 1');
  $element['pass2']['#title'] = t('New title 2');
  return $element;
}

and as a result

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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