Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
13 / 13 |
|
100.00% |
9 / 9 |
CRAP | |
100.00% |
1 / 1 |
| SearchHooks | |
100.00% |
13 / 13 |
|
100.00% |
9 / 9 |
12 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| crmContactInsert | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| crmContactUpdate | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| crmContactDelete | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| crmContactDetailInsert | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| crmContactDetailUpdate | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| crmContactDetailDelete | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| reindexContact | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
2 | |||
| reindexContactFromDetail | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
3 | |||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace Drupal\crm\Hook; |
| 6 | |
| 7 | use Drupal\Core\Hook\Attribute\Hook; |
| 8 | use Drupal\search\SearchIndexInterface; |
| 9 | |
| 10 | /** |
| 11 | * Hooks relating to contacts. |
| 12 | */ |
| 13 | class SearchHooks { |
| 14 | |
| 15 | public function __construct( |
| 16 | protected ?SearchIndexInterface $searchIndex = NULL, |
| 17 | ) {} |
| 18 | |
| 19 | /** |
| 20 | * Implements hook_ENTITY_TYPE_insert(). |
| 21 | */ |
| 22 | #[Hook('crm_contact_insert')] |
| 23 | public function crmContactInsert($contact): void { |
| 24 | $this->reindexContact($contact->id()); |
| 25 | } |
| 26 | |
| 27 | /** |
| 28 | * Implements hook_ENTITY_TYPE_update(). |
| 29 | */ |
| 30 | #[Hook('crm_contact_update')] |
| 31 | public function crmContactUpdate($contact): void { |
| 32 | $this->reindexContact($contact->id()); |
| 33 | } |
| 34 | |
| 35 | /** |
| 36 | * Implements hook_ENTITY_TYPE_delete(). |
| 37 | */ |
| 38 | #[Hook('crm_contact_delete')] |
| 39 | public function crmContactDelete($contact) { |
| 40 | // Mark for removal from search index. |
| 41 | $this->reindexContact($contact->id()); |
| 42 | } |
| 43 | |
| 44 | /** |
| 45 | * Implements hook_ENTITY_TYPE_insert() for crm_contact_detail. |
| 46 | */ |
| 47 | #[Hook('crm_contact_detail_insert')] |
| 48 | public function crmContactDetailInsert($contact_detail): void { |
| 49 | $this->reindexContactFromDetail($contact_detail); |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * Implements hook_ENTITY_TYPE_update() for crm_contact_detail. |
| 54 | */ |
| 55 | #[Hook('crm_contact_detail_update')] |
| 56 | public function crmContactDetailUpdate($contact_detail): void { |
| 57 | $this->reindexContactFromDetail($contact_detail); |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * Implements hook_ENTITY_TYPE_delete() for crm_contact_detail. |
| 62 | */ |
| 63 | #[Hook('crm_contact_detail_delete')] |
| 64 | public function crmContactDetailDelete($contact_detail): void { |
| 65 | $this->reindexContactFromDetail($contact_detail); |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * Marks a contact for reindexing by search module. |
| 70 | * |
| 71 | * @param int|string $contact_id |
| 72 | * The contact ID. |
| 73 | */ |
| 74 | protected function reindexContact(int|string $contact_id): void { |
| 75 | if ($this->searchIndex !== NULL) { |
| 76 | // Reindex contact indexed by the CRM contact search plugin. |
| 77 | $this->searchIndex->markForReindex('crm_contact_search', (int) $contact_id); |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * Marks a contact for reindexing from a contact detail entity. |
| 83 | * |
| 84 | * @param \Drupal\crm\CrmContactDetailInterface $contact_detail |
| 85 | * The contact detail entity. |
| 86 | */ |
| 87 | protected function reindexContactFromDetail($contact_detail): void { |
| 88 | if (!$contact_detail->get('crm_contact')->isEmpty()) { |
| 89 | $contact_id = $contact_detail->get('crm_contact')->target_id; |
| 90 | if ($contact_id) { |
| 91 | $this->reindexContact($contact_id); |
| 92 | } |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | } |