Commit 83631f3f authored by Nikita Sineok's avatar Nikita Sineok Committed by Paulo Henrique Cota Starling
Browse files

Issue #3176611 by nikita_tt, azovsky: Ability to alter status of showing...

Issue #3176611 by nikita_tt, azovsky: Ability to alter status of showing Password Policy element (_password_policy_show_policy)
parent efa64898
Loading
Loading
Loading
Loading
+36 −0
Original line number Diff line number Diff line
<?php

/**
 * @file
 * Hooks and documentation related to password_policy module.
 */

use Drupal\Core\Form\FormStateInterface;

/**
 * @addtogroup hooks
 * @{
 */

/**
 * Alter password policy showing status.
 *
 * @param bool $show_password_policy_status
 *   TRUE - password policy status element will be shown.
 * @param array $context
 *   An associative array containing some context elements. Depends on the place
 *   of parent function ("_password_policy_show_policy") execution.
 */
function hook_password_policy_show_policy_alter(&$show_password_policy_status, array $context) {
  if (isset($context['form_state']) && $context['form_state'] instanceof FormStateInterface) {
    $form_state = $context['form_state'];
    $form_build_info = $form_state->getBuildInfo();
    if (isset($form_build_info['form_id']) && $form_build_info['form_id'] == 'user_notifications_settings_form') {
      $show_password_policy_status = FALSE;
    }
  }
}

/**
 * @} End of "addtogroup hooks".
 */
+9 −3
Original line number Diff line number Diff line
@@ -85,7 +85,7 @@ function password_policy_form_user_form_alter(&$form, FormStateInterface $form_s
  $form['account']['pass']['#weight'] = '3';

  // Check for specific conditions.
  $show_password_policy_status = _password_policy_show_policy();
  $show_password_policy_status = _password_policy_show_policy(get_defined_vars());

  // Load form if relevant.
  if ($show_password_policy_status) {
@@ -130,10 +130,13 @@ function password_policy_element_info_alter(array &$types) {
/**
 * Determine if the password policy should be shown.
 *
 * @param array $context
 *   Context of function execution.
 *
 * @return bool
 *   Result of whether the password policy should be shown.
 */
function _password_policy_show_policy() {
function _password_policy_show_policy(array $context = []) {
  $account = \Drupal::currentUser();
  $config = \Drupal::config('user.settings');
  $show_password_policy_status = TRUE;
@@ -141,6 +144,9 @@ function _password_policy_show_policy() {
  if ($account->isAnonymous() and $config->get('verify_mail')) {
    $show_password_policy_status = FALSE;
  }

  Drupal::moduleHandler()
    ->alter('password_policy_show_policy', $show_password_policy_status, $context);
  return $show_password_policy_status;
}

@@ -161,7 +167,7 @@ function password_policy_check_constraints_password_confirm_process(array $eleme
  $form_object = $form_state->getFormObject();

  if (method_exists($form_object, 'getEntity') && $form_object->getEntity() instanceof UserInterface) {
    if (_password_policy_show_policy()) {
    if (_password_policy_show_policy(get_defined_vars())) {
      $element['pass1']['#ajax'] = [
        'event' => 'change',
        'callback' => '_password_policy_check_constraints',