1

How does one go about changing the Password Tips for the password_confirm form element?

This one specifically:

Recommendations to make your password stronger: 
•Make it at least 12 characters 
•Add lowercase letters
•Add uppercase letters 
•Add numbers 
•Add punctuation

Into something like:

Password should have: 
• Minimum of 12 characters 
• At least one lowercase letter
• At least one uppercase letter
• At least one number
• At least one special character
• No three consecutive same characters

I'm tried using the hook_element_info_alter() hook that calls a method my_module_form_process_password_confirm() and followed this template(https://api.drupal.org/api/drupal/core%21modules%21user%21user.module/function/user_form_process_password_confirm/8.5.x).

But it doesn't seem to be working.

/**
 * Implements hook_element_info_alter().
 */
function client_element_info_alter(array &$types) {
  if (isset($types['password_confirm'])) {
    $types['password_confirm']['#process'][] = 'client_form_process_password_confirm';
  }
}

function client_form_process_password_confirm($element){
  if (($key = array_search('user_form_process_password_confirm', $element['#process'])) !== false) {
    unset($element['#process'][$key]);
  }
  $element['#process'] = array_values($element['#process']);

  $password_settings = [
    'confirmTitle' => t('Passwords match DOES IT:'),
    'confirmSuccess' => t('yes'),
    'confirmFailure' => t('no'),
    'showStrengthIndicator' => FALSE,
  ];

  $password_settings['showStrengthIndicator'] = TRUE;
  $password_settings += [
    'strengthTitle' => t('Password strength:'),
    'hasWeaknesses' => t('Password should have:'),
    'tooShort' => t('Minimum of 12 characters'),
    'addLowerCase' => t('At least one lowercase letter'),
    'addUpperCase' => t('At least one uppercase letter'),
    'addNumbers' => t('At least one number'),
    'addPunctuation' => t('At least one special character'),
    'sameAsUsername' => t('Make it different from your username'),
    'weak' => t('Weak'),
    'fair' => t('Fair'),
    'good' => t('Good'),
    'strong' => t('Strong'),
    'username' => \Drupal::currentUser()->getUsername(),
  ];

  $element['#attached']['library'][] = 'user/drupal.user';
  $element['#attached']['drupalSettings']['password'] = $password_settings;

  return $element;
}
2

so I managed to tinker enough to get something to work and I'm sharing it in case other people need it or something. Of course, I dunno if this is optimal or follows standards or shouldn't even be done in the first place but please feel free to add your input. It's in the my_module.module file, by the by. Oh and don't mind the text, was just kinda testing this out and all that jazz.

Without further ado, here's the code:

/**
 * Implements hook_form_alter().
 */
function <my_module>_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id) {
  if ($form_id === <my_custom_form_id>) {
    // Note that this isn't actually a hook
    $form['#after_build'][] = '<my_module>_confirm_password_after_build';
  }
}

function client_confirm_password_after_build($element) {
  $drupal_settings = $element['pass']['#attached']['drupalSettings']['password'];

  $drupal_settings = [
    'confirmTitle'          => t('Do the passwords match?'),
    'confirmSuccess'        => t('Yeah'),
    'confirmFailure'        => t('Nope'),
    'showStrengthIndicator' => TRUE,
    'hasWeaknesses'         => t('Password Suggestions:'),
    'tooShort'              => t('Make it at least 50 characters'),
    'addLowerCase'          => t('Add 7 lowercase letters'),
    'addUpperCase'          => t('Add 8 uppercase letters'),
    'addNumbers'            => t('Add 8 numbers'),
    'addPunctuation'        => t('Add 10 punctuations'),
    'sameAsUsername'        => t(''),
    'weak' => t('Forgettable'),
    'fair' => t('Meh'),
    'good' => t('Almost'),
    'strong' => t('You got it'),
    'username' => '',
  ];

  $element['pass']['#attached']['drupalSettings']['password'] = $drupal_settings;
  return $element;
}
0

You can use enter link description herepassword_policy

If you wonder why your code isn't working then you should include the code in your question.

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.