Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
75.76% covered (warning)
75.76%
25 / 33
66.67% covered (warning)
66.67%
2 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
ContactHooks
75.76% covered (warning)
75.76%
25 / 33
66.67% covered (warning)
66.67%
2 / 3
6.51
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 crmTheme
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
2
 crmContactDelete
100.00% covered (success)
100.00%
24 / 24
100.00% covered (success)
100.00%
1 / 1
4
1<?php
2
3namespace Drupal\crm\Hook;
4
5use Drupal\Core\Entity\EntityTypeManagerInterface;
6use Drupal\Core\Hook\Attribute\Hook;
7
8/**
9 * Hooks relating to contacts.
10 */
11class ContactHooks {
12
13  public function __construct(
14    protected EntityTypeManagerInterface $entityTypeManager,
15  ) {}
16
17  /**
18   * Implements hook_theme().
19   */
20  #[Hook('crm_theme')]
21  public function crmTheme() {
22    return [
23      'crm_contact' => [
24        'render element' => 'elements',
25      ],
26      'crm_relationship' => [
27        'render element' => 'elements',
28      ],
29    ];
30  }
31
32  /**
33   * Implements hook_ENTITY_TYPE_delete().
34   */
35  #[Hook('crm_contact_delete')]
36  public function crmContactDelete($contact) {
37    $crm_users_contact = $this->entityTypeManager
38      ->getStorage('crm_user_contact')
39      ->loadByProperties(['crm_contact' => $contact->id()]);
40    if ($crm_users_contact != NULL) {
41      $crm_user_contact = reset($crm_users_contact);
42      $crm_user_contact->delete();
43    }
44    // CRM Relationship.
45    $relationship_storage = $this->entityTypeManager
46      ->getStorage('crm_relationship');
47
48    $query = $relationship_storage->getQuery();
49    $query->condition('contacts', $contact->id());
50    $query->accessCheck(FALSE);
51    $relationship_ids = $query->execute();
52
53    foreach ($relationship_ids as $relationship_id) {
54      $relationship = $relationship_storage->load($relationship_id);
55      $relationship->delete();
56    }
57
58    $contact_detail_lists = [];
59    $contact_detail_lists[] = $contact->get('emails');
60    $contact_detail_lists[] = $contact->get('telephones');
61    $contact_detail_lists[] = $contact->get('addresses');
62
63    $contact_detail_storage = $this->entityTypeManager
64      ->getStorage('crm_contact_detail');
65    foreach ($contact_detail_lists as $list) {
66      $detail_entities = $list->referencedEntities();
67      $contact_detail_storage->delete($detail_entities);
68    }
69
70  }
71
72}