Drupal: process order change/ how do I overwrite password suggestion tips

222 views Asked by At

I want to overwrite the password suggestion tip that's configured in Drupal core to show

'Make it at least 14 characters' from 'Make it at least 12 characters'.

Or simply remove the whole password suggestion tips box.

This is the Drupal core code that I want to overwrite https://git.drupalcode.org/project/drupal/-/blob/9.5.0/core/modules/user/user.module#L1109

Below is my function I wrote in my module.

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

function mymodule_form_process_password_confirm($element, $form_state) {
     if (\Drupal::config('user.settings')->get('password_strength')) {
     $password_settings['showStrengthIndicator'] = FALSE;
     $password_settings += [
       'tooShort' => t('Make it at least 14 characters'),
       'username' => \Drupal::currentUser()->getAccountName(),
     ];
   }
  return $element;
}

And this is the result I get if I print out the elements

    [#process] => Array
        (
            [0] => Array
                (
                    [0] => Drupal\Core\Render\Element\PasswordConfirm
                    [1] => processPasswordConfirm
                )

            [1] => mymodule_form_process_password_confirm
            [2] => user_form_process_password_confirm
            [3] => password_policy_check_constraints_password_confirm_process
        )

I think the problem is that the my module's function gets run after the core so it's doing the opposite of what I want it to do.

Do you know how I can run my function after the core? Is there other ways to do it? Or how to remove the whole password suggestion tips from the core?

2

There are 2 answers

0
Trà Dương On

Your module just run before the core.

[1] => mymodule_form_process_password_confirm <== yours
[2] => user_form_process_password_confirm     <== core
[3] => password_policy_check_constraints_password_confirm_process <== password_policy module

The core's module weight is 0, and it has name 'user' is sorted after your module (mymodule). So, add a mymodule.install and set the weight of your module higher than 0 (override core) or higher than 10 (override password_policy also) The function you need to call is module_set_weight, refer to https://api.drupal.org/api/drupal/core%21includes%21module.inc/function/module_set_weight/9.0.x

Your mymodule.install should be something like:

<?php

/**
 * Implements hook_install().
 */
function mymodule_install() {
  // Your desired weight, 11 is override the password_policy, or just 1 to override core.
  module_set_weight('mymodule', 11);
}
0
2pha On

In your code you're setting the variable $password_settings, and returning $element..... you have not changed $element at all.
try something like this (untested)..

function mymodule_form_process_password_confirm($element, $form_state) {
   if (isset($element['#attached']['drupalSettings']['password'])) {
     $element['#attached']['drupalSettings']['password']['showStrengthIndicator'] = FALSE;
     $element['#attached']['drupalSettings']['password']['tooShort'] = t('Make it at least 14 characters')
   }
  return $element;
}