-
Notifications
You must be signed in to change notification settings - Fork 0
/
sharedemail.module
executable file
·65 lines (58 loc) · 1.85 KB
/
sharedemail.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
<?php
/**
* @file
* Shared email module file.
*/
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Form\FormStateInterface;
/**
* Implements hook_help().
*/
function sharedemail_help($route_name) {
switch ($route_name) {
case 'help.page.sharedemail':
$output = '<p>' . t('Allow an e-mail address to be used by more than one user account.') . '</p>';
return $output;
}
}
/**
* Implements hook_entity_base_field_info_alter().
*
* Removes the unique constraint for the email address.
*/
function sharedemail_entity_base_field_info_alter(&$fields, EntityTypeInterface $entity_type) {
// Alter the email text field to allow duplicates.
if (!empty($fields['mail']) && $entity_type->id() === 'user') {
$constraints = $fields['mail']->getConstraints();
unset($constraints['UserMailUnique']);
$constraints['SharedEmailUnique'] = [];
$fields['mail']->setConstraints($constraints);
}
}
/**
* Implements hook_form_FORM_ID_alter().
*
* FORM_ID = user_profile_form
* Add a submit check to see.
*/
function sharedemail_form_user_form_alter(&$form, FormStateInterface $form_state, $form_id) {
$form['actions']['submit']['#submit'][] = '_sharedemail_form_submit';
}
/**
* Shared email custom submit handler.
*
* If the user has access to be shown the message and the message applies, show
* it.
*/
function _sharedemail_form_submit($form, FormStateInterface $form_state) {
// Do they have permission to see the message.
if (\Drupal::currentUser()->hasPermission('access shared email message')) {
$users = \Drupal::entityTypeManager()
->getStorage('user')
->loadByProperties(['mail' => $form_state->getValue('mail')]);
if ($users !== NULL && count($users) > 1) {
$config = \Drupal::config('sharedemail.settings');
\Drupal::messenger()->addWarning($config->get('sharedemail_msg'));
}
}
}