Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
99.41% |
169 / 170 |
|
50.00% |
1 / 2 |
CRAP | |
0.00% |
0 / 1 |
Contact | |
99.41% |
169 / 170 |
|
50.00% |
1 / 2 |
4 | |
0.00% |
0 / 1 |
preSave | |
94.12% |
16 / 17 |
|
0.00% |
0 / 1 |
3.00 | |||
baseFieldDefinitions | |
100.00% |
153 / 153 |
|
100.00% |
1 / 1 |
1 |
1 | <?php |
2 | |
3 | declare(strict_types=1); |
4 | |
5 | namespace Drupal\crm\Entity; |
6 | |
7 | use Drupal\Core\Entity\Attribute\ContentEntityType; |
8 | use Drupal\Core\Entity\EntityChangedTrait; |
9 | use Drupal\Core\Entity\EntityPublishedTrait; |
10 | use Drupal\Core\Entity\EntityStorageInterface; |
11 | use Drupal\Core\Entity\EntityTypeInterface; |
12 | use Drupal\Core\Entity\RevisionableContentEntityBase; |
13 | use Drupal\Core\Entity\RevisionLogEntityTrait; |
14 | use Drupal\Core\Entity\Routing\AdminHtmlRouteProvider; |
15 | use Drupal\Core\Field\BaseFieldDefinition; |
16 | use Drupal\Core\StringTranslation\TranslatableMarkup; |
17 | use Drupal\crm\ContactAccessControlHandler; |
18 | use Drupal\crm\ContactListBuilder; |
19 | use Drupal\crm\CrmContactInterface; |
20 | use Drupal\crm\Form\ContactForm; |
21 | use Drupal\inline_entity_form\Plugin\Field\FieldWidget\InlineEntityFormComplex; |
22 | use Drupal\views\EntityViewsData; |
23 | use Drupal\Core\Entity\ContentEntityDeleteForm; |
24 | |
25 | /** |
26 | * CRM contact. |
27 | * |
28 | * A contact can be a person, an organization, a household, etc. |
29 | */ |
30 | #[ContentEntityType( |
31 | id: 'crm_contact', |
32 | label: new TranslatableMarkup('CRM Contact'), |
33 | label_collection: new TranslatableMarkup('CRM Contacts'), |
34 | label_singular: new TranslatableMarkup('crm contact'), |
35 | label_plural: new TranslatableMarkup('crm contacts'), |
36 | label_count: [ |
37 | 'singular' => '@count crm contact', |
38 | 'plural' => '@count crm contacts', |
39 | ], |
40 | bundle_label: new TranslatableMarkup('Contact type'), |
41 | handlers: [ |
42 | 'list_builder' => ContactListBuilder::class, |
43 | 'views_data' => EntityViewsData::class, |
44 | 'access' => ContactAccessControlHandler::class, |
45 | 'form' => [ |
46 | 'default' => ContactForm::class, |
47 | 'delete' => ContentEntityDeleteForm::class, |
48 | ], |
49 | 'route_provider' => [ |
50 | 'html' => AdminHtmlRouteProvider::class, |
51 | ], |
52 | ], |
53 | base_table: 'crm_contact', |
54 | revision_table: 'crm_contact_revision', |
55 | show_revision_ui: TRUE, |
56 | bundle_entity_type: 'crm_contact_type', |
57 | field_ui_base_route: 'entity.crm_contact_type.edit_form', |
58 | translatable: FALSE, |
59 | admin_permission: 'administer crm', |
60 | entity_keys: [ |
61 | 'id' => 'id', |
62 | 'revision' => 'revision_id', |
63 | 'bundle' => 'bundle', |
64 | 'label' => 'name', |
65 | 'uuid' => 'uuid', |
66 | 'status' => 'status', |
67 | 'published' => 'status', |
68 | ], |
69 | revision_metadata_keys: [ |
70 | 'revision_user' => 'revision_uid', |
71 | 'revision_created' => 'revision_timestamp', |
72 | 'revision_log_message' => 'revision_log', |
73 | ], |
74 | links: [ |
75 | 'add-page' => '/crm/contact/add', |
76 | 'add-form' => '/crm/contact/add/{crm_contact_type}', |
77 | 'canonical' => '/crm/contact/{crm_contact}', |
78 | 'edit-form' => '/crm/contact/{crm_contact}/edit', |
79 | 'delete-form' => '/crm/contact/{crm_contact}/delete', |
80 | 'collection' => '/admin/content/crm/contact', |
81 | ], |
82 | )] |
83 | class Contact extends RevisionableContentEntityBase implements CrmContactInterface { |
84 | use EntityChangedTrait; |
85 | use EntityPublishedTrait; |
86 | use RevisionLogEntityTrait; |
87 | |
88 | /** |
89 | * {@inheritdoc} |
90 | */ |
91 | public function preSave(EntityStorageInterface $storage) { |
92 | parent::preSave($storage); |
93 | $this->setNewRevision(); |
94 | |
95 | // If type is person, set the label field to the name field. |
96 | if ($this->bundle() == 'person') { |
97 | $name_array = $this->get('full_name')->getValue(); |
98 | if (empty($name_array)) { |
99 | return; |
100 | } |
101 | $name_array = $name_array[0]; |
102 | $name_array['preferred'] = $this->get('preferred_name')->value; |
103 | $name_array['alternative'] = array_map(function ($alias) { |
104 | return $alias['value']; |
105 | }, $this->get('aliases')->getValue()); |
106 | $name_array['alternative'] = implode(', ', $name_array['alternative']); |
107 | $name_format = $this->bundle->entity->getThirdPartySetting('crm', 'name_format') ?? 'default'; |
108 | |
109 | $name_formatter = \Drupal::service('name.formatter'); |
110 | $formatted_name = $name_formatter->format($name_array, $name_format); |
111 | |
112 | $formatted_name = html_entity_decode($formatted_name, ENT_QUOTES | ENT_HTML5, 'UTF-8'); |
113 | $this->set('name', $formatted_name); |
114 | |
115 | } |
116 | |
117 | } |
118 | |
119 | /** |
120 | * {@inheritdoc} |
121 | */ |
122 | public static function baseFieldDefinitions(EntityTypeInterface $entity_type) { |
123 | $fields = parent::baseFieldDefinitions($entity_type); |
124 | |
125 | $fields['name'] = BaseFieldDefinition::create('string') |
126 | ->setRevisionable(TRUE) |
127 | ->setLabel(t('Name')) |
128 | ->setRequired(TRUE) |
129 | ->setSetting('max_length', 255) |
130 | ->setDisplayOptions('form', [ |
131 | 'type' => 'string_textfield', |
132 | 'weight' => -5, |
133 | ]) |
134 | ->setDisplayConfigurable('form', TRUE) |
135 | ->setDisplayOptions('view', [ |
136 | 'label' => 'hidden', |
137 | 'type' => 'string', |
138 | 'weight' => -5, |
139 | ]) |
140 | ->setDisplayConfigurable('view', TRUE); |
141 | |
142 | $fields['emails'] = BaseFieldDefinition::create('entity_reference') |
143 | ->setLabel(t('Emails')) |
144 | ->setRevisionable(TRUE) |
145 | ->setCardinality(-1) |
146 | ->setSetting('target_type', 'crm_contact_detail') |
147 | ->setSetting('handler_settings', ['target_bundles' => ['email']]) |
148 | ->setDisplayConfigurable('form', TRUE) |
149 | ->setDisplayOptions('form', [ |
150 | 'type' => 'inline_entity_form_complex', |
151 | 'weight' => 8, |
152 | 'settings' => [ |
153 | 'allow_new' => TRUE, |
154 | 'allow_existing' => FALSE, |
155 | 'removed_reference' => InlineEntityFormComplex::REMOVED_DELETE, |
156 | 'match_operator' => 'CONTAINS', |
157 | 'allow_duplicate' => FALSE, |
158 | 'form_mode' => 'default', |
159 | 'override_labels' => TRUE, |
160 | 'label_singular' => t('email'), |
161 | 'label_plural' => t('emails'), |
162 | 'collapsible' => FALSE, |
163 | 'collapsed' => FALSE, |
164 | 'revision' => TRUE, |
165 | ], |
166 | ]) |
167 | ->setDisplayConfigurable('view', TRUE) |
168 | ->setDisplayOptions('view', [ |
169 | 'label' => 'hidden', |
170 | 'type' => 'entity_reference_entity_view', |
171 | 'weight' => -5, |
172 | 'settings' => [ |
173 | 'view_mode' => 'default', |
174 | 'link' => FALSE, |
175 | ], |
176 | ]); |
177 | |
178 | $fields['telephones'] = BaseFieldDefinition::create('entity_reference') |
179 | ->setLabel(t('Telephones')) |
180 | ->setRevisionable(TRUE) |
181 | ->setCardinality(-1) |
182 | ->setSetting('target_type', 'crm_contact_detail') |
183 | ->setSetting('handler_settings', ['target_bundles' => ['telephone']]) |
184 | ->setDisplayConfigurable('form', TRUE) |
185 | ->setDisplayOptions('form', [ |
186 | 'type' => 'inline_entity_form_complex', |
187 | 'weight' => 9, |
188 | 'settings' => [ |
189 | 'allow_new' => TRUE, |
190 | 'allow_existing' => FALSE, |
191 | 'removed_reference' => InlineEntityFormComplex::REMOVED_DELETE, |
192 | 'match_operator' => 'CONTAINS', |
193 | 'allow_duplicate' => FALSE, |
194 | 'form_mode' => 'default', |
195 | 'override_labels' => TRUE, |
196 | 'label_singular' => t('telephone'), |
197 | 'label_plural' => t('telephones'), |
198 | 'collapsible' => FALSE, |
199 | 'collapsed' => FALSE, |
200 | 'revision' => TRUE, |
201 | ], |
202 | ]) |
203 | ->setDisplayConfigurable('view', TRUE) |
204 | ->setDisplayOptions('view', [ |
205 | 'label' => 'hidden', |
206 | 'type' => 'entity_reference_entity_view', |
207 | 'weight' => -5, |
208 | 'settings' => [ |
209 | 'view_mode' => 'default', |
210 | 'link' => FALSE, |
211 | ], |
212 | ]); |
213 | |
214 | $fields['addresses'] = BaseFieldDefinition::create('entity_reference') |
215 | ->setLabel('Addresses') |
216 | ->setRevisionable(TRUE) |
217 | ->setCardinality(-1) |
218 | ->setSetting('target_type', 'crm_contact_detail') |
219 | ->setSetting('handler_settings', ['target_bundles' => ['address']]) |
220 | ->setDisplayConfigurable('form', TRUE) |
221 | ->setDisplayOptions('form', [ |
222 | 'type' => 'inline_entity_form_complex', |
223 | 'weight' => 10, |
224 | 'settings' => [ |
225 | 'allow_new' => TRUE, |
226 | 'allow_existing' => FALSE, |
227 | 'removed_reference' => InlineEntityFormComplex::REMOVED_DELETE, |
228 | 'match_operator' => 'CONTAINS', |
229 | 'allow_duplicate' => FALSE, |
230 | 'form_mode' => 'default', |
231 | 'override_labels' => TRUE, |
232 | 'label_singular' => t('address'), |
233 | 'label_plural' => t('addresses'), |
234 | 'collapsible' => FALSE, |
235 | 'collapsed' => FALSE, |
236 | 'revision' => TRUE, |
237 | ], |
238 | ]) |
239 | ->setDisplayConfigurable('view', TRUE) |
240 | ->setDisplayOptions('view', [ |
241 | 'label' => 'hidden', |
242 | 'type' => 'entity_reference_entity_view', |
243 | 'weight' => 0, |
244 | 'settings' => [ |
245 | 'view_mode' => 'default', |
246 | 'link' => FALSE, |
247 | ], |
248 | ]); |
249 | |
250 | $fields['status'] = BaseFieldDefinition::create('boolean') |
251 | ->setLabel(t('Status')) |
252 | ->setDescription(t('A boolean indicating whether the contact is active.')) |
253 | ->setDefaultValue(TRUE) |
254 | ->setDisplayOptions('form', [ |
255 | 'type' => 'boolean_checkbox', |
256 | 'settings' => [ |
257 | 'display_label' => TRUE, |
258 | ], |
259 | 'weight' => 120, |
260 | ]) |
261 | ->setSetting('on_label', 'Status') |
262 | ->setDisplayConfigurable('form', TRUE); |
263 | |
264 | $fields['created'] = BaseFieldDefinition::create('created') |
265 | ->setLabel(t('Created on')) |
266 | ->setDescription(t('The time that the contact was created.')) |
267 | ->setDisplayOptions('view', [ |
268 | 'label' => 'above', |
269 | 'type' => 'timestamp', |
270 | 'weight' => 20, |
271 | ]) |
272 | ->setDisplayConfigurable('form', TRUE) |
273 | ->setDisplayOptions('form', [ |
274 | 'type' => 'datetime_timestamp', |
275 | 'weight' => 20, |
276 | ]) |
277 | ->setDisplayConfigurable('view', TRUE); |
278 | |
279 | $fields['changed'] = BaseFieldDefinition::create('changed') |
280 | ->setLabel(t('Changed')) |
281 | ->setDescription(t('The time that the contact was last edited.')); |
282 | |
283 | return $fields; |
284 | } |
285 | |
286 | } |