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