You are here

function better_passwords_after_build in Better Passwords 8

Same name and namespace in other branches
  1. 2.x better_passwords.module \better_passwords_after_build()

After build function for password_confirm elements.

Parameters

array $element: The element being altered after build.

\Drupal\Core\Form\FormStateInterface $form_state: The form_state object returned with the element.

1 string reference to 'better_passwords_after_build'
better_passwords_element_info_alter in ./better_passwords.module
Implements hook_element_info_alter().

File

./better_passwords.module, line 29
The better passwords module file.

Code

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

  // Hide the password fields if Better Passwords is generating passwords.
  if (($generate = $config
    ->get('auto_generate')) && \Drupal::currentUser()
    ->isAuthenticated() && $form_state
    ->getFormObject()
    ->getFormId() == 'user_register_form') {
    $element['#required'] = $element['pass1']['#required'] = $element['pass2']['#required'] = FALSE;
    if ($generate == 1) {
      $element['pass1']['#states']['visible']['#auto-generate-password'] = $element['pass2']['#states']['visible']['#auto-generate-password'] = [
        'checked' => FALSE,
      ];
      $element['auto_generate_password'] = [
        '#type' => 'checkbox',
        '#title' => t('Auto-generate password'),
        '#checked' => TRUE,
        '#attributes' => [
          'id' => 'auto-generate-password',
        ],
        '#parents' => $element['#parents'],
        '#array_parents' => $element['#array_parents'],
      ];
    }
    else {
      $element['pass1']['#access'] = $element['pass2']['#access'] = FALSE;
    }
  }

  // Better Password validate should come before other validate functions.
  array_unshift($element['#element_validate'], 'better_passwords_validate');
  return $element;
}