Warning: Undefined array key "#description" in better_passwords_after_build() (line 33 of modules/contrib/better_passwords/better_passwords.module)

Created on 8 October 2025, 14 days ago

Problem/Motivation

The new code (see https://www.drupal.org/project/better_passwords/issues/3341916 the minimum requirement isn't communicated upfront nor announced in screenreader on fail Needs review ) to add the password length and strength text to password confirm elements is not checking that the '#description' key exists before trying to append a string to it.

We are using the https://www.drupal.org/project/entity_access_password module to add a password field to a content type and now on every load of the edit page we get a warning.

Warning: Undefined array key "#description" in better_passwords_after_build() (line 33 of modules/contrib/better_passwords/better_passwords.module).

I guess this would also be true of any custom forms in any module that uses the password_confirm field and does not set a description for the element.

Proposed resolution

The code in better_passwords_after_build needs to check that $element['#description'] exists before trying to add additional text. Additionally, the first br tag is only required if there is existing content.

function better_passwords_after_build(array $element, FormStateInterface $form_state) {
  $config = \Drupal::config('better_passwords.settings');

  if (!isset($element['#description'])) {
    $element['#description'] = '';
  }
  // Set extra info on password_confirm description.
  if (!empty($element['#description'])) {
    $element['#description'] .= '<br/>' ;
  }
  $element['#description'] .= t(
    'Passwords must be at least @num characters.',
    ['@num' => $config->get('length')]
  );
  if (!empty($config->get('strength'))) {
    $element['#description'] .= '<br/>' . t('Passwords will be rated for their strength.');
  }
🐛 Bug report
Status

Active

Version

2.2

Component

Code

Created by

🇬🇧United Kingdom altcom_neil

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Merge Requests

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

Production build 0.71.5 2024