-
Notifications
You must be signed in to change notification settings - Fork 0
/
nocurrent_pass.module
78 lines (64 loc) · 2.76 KB
/
nocurrent_pass.module
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
<?php
/**
* @file Provide settings to be able to disable the Current password field on the user edit page
*
* Alter the Account settings page (Configuration > People > Account settings) and the user edit page.
*/
use Drupal\Component\Utility\Crypt;
use Drupal\Core\Form\FormStateInterface;
/**
* Implements hook_form_FORM_ID_alter().
*
* Adds a checkbox inside a fieldset to the Account settings page (Configuration > People > Account settings)
* where we can disable or enable the Current Password field on user edit page.
*/
function nocurrent_pass_form_user_admin_settings_alter(&$form, &$form_state) {
// Get configuration
$nocurrent_pass_disabled = \Drupal::config('nocurrent_pass.settings')->get('nocurrent_pass_disabled');
// Reorder fieldset weights
$form['anonymous_settings']['#weight'] = -4;
$form['admin_role']['#weight'] = -3;
$form['registration_cancellation']['#weight'] = -2;
// Add "Do not require current password" field and its fieldset
$form['nocurrent_pass_settings'] = array(
'#type' => 'fieldset',
'#title' => t('Require Current Password'),
'#weight' => -1,
);
$form['nocurrent_pass_settings']['nocurrent_pass_disabled'] = array(
'#type' => 'checkbox',
'#title' => t('Do not require current password'),
'#description' => t('Check this box to disable the "current password" field on the User Edit form.'),
'#default_value' => $nocurrent_pass_disabled,
);
// Add submit function which saves the settings into configuration
$form['#submit'][] = 'nocurrent_pass_form_user_admin_settings_submit';
}
/**
* Submit - "Save the Do not require current password" field's vale into configuration
*/
function nocurrent_pass_form_user_admin_settings_submit(array &$form, FormStateInterface $form_state) {
$config = \Drupal::service('config.factory')->getEditable('nocurrent_pass.settings');
$config->set('nocurrent_pass_disabled', $form_state->getValue('nocurrent_pass_disabled'));
$config->save();
}
/**
* Implements hook_form_FORM_ID_alter().
* Remove the current password field from the user_profile_form form (user/%/edit).
*/
function nocurrent_pass_form_user_form_alter(&$form, &$form_state) {
// Get the module's configuration
$config = \Drupal::config('nocurrent_pass.settings');
$nocurrent_pass_disabled = $config->get('nocurrent_pass_disabled');
// If the Current password is disabled
if ($nocurrent_pass_disabled) {
$account = \Drupal::service('current_route_match')->getParameter('user');
// If this is not the Superadmin's edit page
if ( !empty($account) && ($account->id() != 1) ) {
// Turn off Current password field's validation
$form_state->set('user_pass_reset', 1);
// Hide the Current password fields
$form['account']['current_pass']['#access'] = FALSE;
}
}
}