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