Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
66.67% |
22 / 33 |
|
50.00% |
2 / 4 |
CRAP | |
0.00% |
0 / 1 |
UserHooks | |
66.67% |
22 / 33 |
|
50.00% |
2 / 4 |
13.70 | |
0.00% |
0 / 1 |
__construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
userInsert | |
88.24% |
15 / 17 |
|
0.00% |
0 / 1 |
4.03 | |||
userContactFormatNameAlter | |
0.00% |
0 / 9 |
|
0.00% |
0 / 1 |
12 | |||
userDelete | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
2 |
1 | <?php |
2 | |
3 | namespace Drupal\crm\Hook; |
4 | |
5 | use Drupal\Core\Config\ConfigFactoryInterface; |
6 | use Drupal\Core\Entity\EntityTypeManagerInterface; |
7 | use Drupal\Core\Hook\Attribute\Hook; |
8 | use Drupal\Core\Session\AccountInterface; |
9 | use Drupal\crm\Event\CrmUserContactEvent; |
10 | use Drupal\user\UserInterface; |
11 | use Symfony\Contracts\EventDispatcher\EventDispatcherInterface; |
12 | |
13 | /** |
14 | * Hooks relating to users. |
15 | */ |
16 | class UserHooks { |
17 | |
18 | public function __construct( |
19 | protected ConfigFactoryInterface $configFactory, |
20 | protected EntityTypeManagerInterface $entityTypeManager, |
21 | protected EventDispatcherInterface $eventDispatcher, |
22 | ) {} |
23 | |
24 | /** |
25 | * Implements hook_ENTITY_TYPE_insert(). |
26 | */ |
27 | #[Hook('user_insert')] |
28 | public function userInsert(UserInterface $user) { |
29 | |
30 | $auto_create_crm_user_contact = $this->configFactory |
31 | ->get('crm.crm_user_contact.settings') |
32 | ->get('auto_create_crm_user_contact'); |
33 | if (!$auto_create_crm_user_contact) { |
34 | return; |
35 | } |
36 | |
37 | $crm_user_contact = $this->entityTypeManager |
38 | ->getStorage('crm_user_contact') |
39 | ->create(['user' => $user->id()]); |
40 | $event = new CrmUserContactEvent($crm_user_contact); |
41 | $this->eventDispatcher->dispatch($event, CrmUserContactEvent::EVENT_NAME); |
42 | $crm_user_contact = $event->getCrmUserContact(); |
43 | $contact = $crm_user_contact->getContact(); |
44 | |
45 | if ($contact == NULL) { |
46 | return NULL; |
47 | } |
48 | if ($contact->get('bundle')->target_id != 'person') { |
49 | return NULL; |
50 | } |
51 | |
52 | $crm_user_contact->save(); |
53 | } |
54 | |
55 | /** |
56 | * Implements hook_user_format_name_alter(). |
57 | */ |
58 | #[Hook('user_contact_format_name_alter')] |
59 | public function userContactFormatNameAlter(&$name, AccountInterface $account) { |
60 | $override = $this->configFactory->get('crm.crm_user_contact.settings')->get('display_name'); |
61 | if (!$override) { |
62 | return; |
63 | } |
64 | $crm_users_contact = $this->entityTypeManager |
65 | ->getStorage('crm_user_contact') |
66 | ->loadByProperties(['user' => $account->id()]); |
67 | if ($crm_users_contact != NULL) { |
68 | /** @var \Drupal\crm\Entity\UserContact */ |
69 | $crm_user_contact = reset($crm_users_contact); |
70 | $name = $crm_user_contact->getContact()?->label(); |
71 | } |
72 | } |
73 | |
74 | /** |
75 | * Implements hook_ENTITY_TYPE_delete(). |
76 | */ |
77 | #[Hook('user_delete')] |
78 | public function userDelete(UserInterface $user) { |
79 | $crm_users_contact = $this->entityTypeManager |
80 | ->getStorage('crm_user_contact') |
81 | ->loadByProperties(['user' => $user->id()]); |
82 | if ($crm_users_contact != NULL) { |
83 | $crm_user_contact = reset($crm_users_contact); |
84 | $crm_user_contact->delete(); |
85 | } |
86 | } |
87 | |
88 | } |