Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
75.56% covered (warning)
75.56%
102 / 135
41.67% covered (danger)
41.67%
5 / 12
CRAP
n/a
0 / 0
crm_contact_theme
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
2
template_preprocess_crm_contact
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
6
template_preprocess_crm_relationship
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
6
crm_views_data_alter
100.00% covered (success)
100.00%
45 / 45
100.00% covered (success)
100.00%
1 / 1
1
crm_user_insert
88.24% covered (warning)
88.24%
15 / 17
0.00% covered (danger)
0.00%
0 / 1
4.03
crm_user_contact_format_name_alter
0.00% covered (danger)
0.00%
0 / 9
0.00% covered (danger)
0.00%
0 / 1
12
crm_user_delete
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
2
crm_crm_contact_delete
100.00% covered (success)
100.00%
23 / 23
100.00% covered (success)
100.00%
1 / 1
3
crm_entity_bundle_info_alter
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
4
crm_inline_entity_form_table_fields_alter
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
6
crm_entity_view
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
20
crm_form_block_form_alter
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2
3/**
4 * @file
5 * Provides a contact entity type.
6 */
7
8use Drupal\Core\Entity\EntityInterface;
9use Drupal\Core\Form\FormStateInterface;
10use Drupal\Core\Render\Element;
11use Drupal\Core\Session\AccountInterface;
12use Drupal\crm\Entity\ContactDetail\AddressContactDetail;
13use Drupal\crm\Entity\ContactDetail\EmailContactDetail;
14use Drupal\crm\Entity\ContactDetail\TelephoneContactDetail;
15use Drupal\crm\Event\CrmUserContactEvent;
16use Drupal\user\UserInterface;
17
18/**
19 * Implements hook_theme().
20 */
21function crm_contact_theme() {
22  return [
23    'crm_contact' => [
24      'render element' => 'elements',
25    ],
26    'crm_relationship' => [
27      'render element' => 'elements',
28    ],
29  ];
30}
31
32/**
33 * Prepares variables for contact templates.
34 *
35 * Default template: crm-contact.html.twig.
36 *
37 * @param array $variables
38 *   An associative array containing:
39 *   - elements: An associative array containing the contact information and any
40 *     fields attached to the entity.
41 *   - attributes: HTML attributes for the containing element.
42 */
43function template_preprocess_crm_contact(array &$variables) {
44  $variables['view_mode'] = $variables['elements']['#view_mode'];
45  foreach (Element::children($variables['elements']) as $key) {
46    $variables['content'][$key] = $variables['elements'][$key];
47  }
48}
49
50/**
51 * Prepares variables for crm relationship templates.
52 *
53 * Default template: crm-relationship.html.twig.
54 *
55 * @param array $variables
56 *   An associative array containing:
57 *   - elements: An associative array containing the relationship and any
58 *     fields attached to the entity.
59 *   - attributes: HTML attributes for the containing element.
60 */
61function template_preprocess_crm_relationship(array &$variables) {
62  $variables['view_mode'] = $variables['elements']['#view_mode'];
63  foreach (Element::children($variables['elements']) as $key) {
64    $variables['content'][$key] = $variables['elements'][$key];
65  }
66}
67
68/**
69 * Implements hook_views_data_alter().
70 *
71 * @todo Remove once https://www.drupal.org/project/drupal/issues/2706431 is
72 * resolved.
73 */
74function crm_views_data_alter(array &$data) {
75  $title = t('CRM User Sync Relation');
76  $data['users_field_data']['crm_user_contact'] = [
77    'title' => $title,
78    'help' => t('Adds relation to CRM User Sync Relation that point to current user.'),
79    'relationship' => [
80      'base' => 'crm_user_contact',
81      'base field' => 'user',
82      'field' => 'uid',
83      'id' => 'standard',
84      'label' => $title,
85    ],
86  ];
87
88  $data['users_field_data']['crm_user_contact_sync_form'] = [
89    'title' => t('Contact relation'),
90    'help' => t("Provides a link to the user's contact relation form."),
91    'field' => [
92      'id' => 'crm_contact_user',
93    ],
94  ];
95  $data['users_field_data']['crm_core_user_sync_form'] = [
96    'title' => t('Contact relation'),
97    'help' => t("Provides a link to the user's contact relation form."),
98    'field' => [
99      'id' => 'crm_user_contact',
100    ],
101  ];
102
103  $data['crm_contact']['crm_user_contact'] = [
104    'title' => $title,
105    'help' => t('Adds relation to CRM User Sync Relation that point to current person.'),
106    'relationship' => [
107      'base' => 'crm_user_contact',
108      'base field' => 'crm_contact',
109      'field' => 'id',
110      'id' => 'standard',
111      'label' => $title,
112    ],
113  ];
114
115  $data['crm_contact']['crm_core_user_sync_form'] = [
116    'title' => t('User relation'),
117    'help' => t("Provides a link to the person's user relation form."),
118    'field' => [
119      'id' => 'crm_user_contact',
120      'field_name' => 'user',
121    ],
122  ];
123
124}
125
126/**
127 * Implements hook_ENTITY_TYPE_insert().
128 */
129function crm_user_insert(UserInterface $user) {
130
131  $auto_create_crm_user_contact = \Drupal::config('crm.crm_user_contact.settings')
132    ->get('auto_create_crm_user_contact');
133  if (!$auto_create_crm_user_contact) {
134    return;
135  }
136
137  $crm_user_contact = \Drupal::entityTypeManager()
138    ->getStorage('crm_user_contact')
139    ->create(['user' => $user->id()]);
140  $event = new CrmUserContactEvent($crm_user_contact);
141  \Drupal::service('event_dispatcher')
142    ->dispatch($event, CrmUserContactEvent::EVENT_NAME);
143  $crm_user_contact = $event->getCrmUserContact();
144  $contact = $crm_user_contact->getContact();
145
146  if ($contact == NULL) {
147    return NULL;
148  }
149  if ($contact->get('bundle')->target_id != 'person') {
150    return NULL;
151  }
152
153  $crm_user_contact->save();
154}
155
156/**
157 * Implements hook_user_format_name_alter().
158 */
159function crm_user_contact_format_name_alter(&$name, AccountInterface $account) {
160  $override = \Drupal::config('crm.crm_user_contact.settings')->get('display_name');
161  if (!$override) {
162    return;
163  }
164  $crm_users_contact = \Drupal::entityTypeManager()
165    ->getStorage('crm_user_contact')
166    ->loadByProperties(['user' => $account->id()]);
167  if ($crm_users_contact != NULL) {
168    $crm_user_contact = reset($crm_users_contact);
169    $name = $crm_user_contact->getContact()?->label();
170  }
171}
172
173/**
174 * Implements hook_ENTITY_TYPE_delete().
175 */
176function crm_user_delete(UserInterface $user) {
177  $crm_users_contact = \Drupal::entityTypeManager()
178    ->getStorage('crm_user_contact')
179    ->loadByProperties(['user' => $user->id()]);
180  if ($crm_users_contact != NULL) {
181    $crm_user_contact = reset($crm_users_contact);
182    $crm_user_contact->delete();
183  }
184}
185
186/**
187 * Implements hook_ENTITY_TYPE_delete().
188 */
189function crm_crm_contact_delete($contact) {
190  $crm_users_contact = \Drupal::entityTypeManager()
191    ->getStorage('crm_user_contact')
192    ->loadByProperties(['crm_contact' => $contact->id()]);
193  if ($crm_users_contact != NULL) {
194    $crm_user_contact = reset($crm_users_contact);
195    $crm_user_contact->delete();
196  }
197  // CRM Relationship.
198  $relationship_storage = \Drupal::entityTypeManager()
199    ->getStorage('crm_relationship');
200
201  $relationships_a = $relationship_storage
202    ->loadByProperties(['contact_a' => $contact->id()]);
203  $relationship_storage->delete($relationships_a);
204
205  $relationships_b = $relationship_storage
206    ->loadByProperties(['contact_b' => $contact->id()]);
207  $relationship_storage->delete($relationships_b);
208
209  $contact_detail_lists = [];
210  $contact_detail_lists[] = $contact->get('emails');
211  $contact_detail_lists[] = $contact->get('telephones');
212  $contact_detail_lists[] = $contact->get('addresses');
213
214  $contact_detail_storage = \Drupal::entityTypeManager()
215    ->getStorage('crm_contact_detail');
216  foreach ($contact_detail_lists as $list) {
217    $detail_entities = $list->referencedEntities();
218    $contact_detail_storage->delete($detail_entities);
219  }
220
221}
222
223/**
224 * Implements hook_entity_bundle_info_alter().
225 */
226function crm_entity_bundle_info_alter(array &$bundles): void {
227  if (isset($bundles['crm_contact_detail']['address'])) {
228    $bundles['crm_contact_detail']['address']['class'] = AddressContactDetail::class;
229  }
230  if (isset($bundles['crm_contact_detail']['email'])) {
231    $bundles['crm_contact_detail']['email']['class'] = EmailContactDetail::class;
232  }
233  if (isset($bundles['crm_contact_detail']['telephone'])) {
234    $bundles['crm_contact_detail']['telephone']['class'] = TelephoneContactDetail::class;
235  }
236}
237
238/**
239 * Implements hook_inline_entity_form_table_fields_alter().
240 */
241function crm_inline_entity_form_table_fields_alter(&$fields, $context) {
242  if ($context['parent_entity_type'] == 'crm_contact' && $context['entity_type'] == 'crm_contact_detail') {
243    if ($context['allowed_bundles'] == ['address']) {
244      $fields['label']['label'] = t('Address (line 1)');
245    }
246    elseif ($context['allowed_bundles'] == ['telephone']) {
247      $fields['label']['label'] = t('Telephone');
248    }
249    elseif ($context['allowed_bundles'] == ['email']) {
250      $fields['label']['label'] = t('Email');
251    }
252  }
253}
254
255/**
256 * Implements hook_entity_view().
257 */
258function crm_entity_view(array &$build, EntityInterface $entity, $view_mode, $langcode) {
259  $entity_type = $entity->getEntityTypeId();
260
261  if ($entity_type === 'crm_relationship') {
262    $bundle = $entity->get('bundle')->entity;
263    if (isset($build['contact_a'])) {
264      $build['contact_a']['#title'] = $bundle->get('label_a');
265    }
266    if (isset($build['contact_b'])) {
267      $build['contact_b']['#title'] = $bundle->get('label_b');
268    }
269  }
270}
271
272/**
273 * Implements hook_form_FORM_ID_alter().
274 */
275function crm_form_block_form_alter(&$form, FormStateInterface $form_state) {
276  $form['#attached']['library'][] = 'crm/crm.block';
277}