Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
86.96% |
60 / 69 |
|
50.00% |
1 / 2 |
CRAP | |
0.00% |
0 / 1 |
ContactDetail | |
86.96% |
60 / 69 |
|
50.00% |
1 / 2 |
3.02 | |
0.00% |
0 / 1 |
baseFieldDefinitions | |
100.00% |
60 / 60 |
|
100.00% |
1 / 1 |
1 | |||
label | |
0.00% |
0 / 9 |
|
0.00% |
0 / 1 |
6 |
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\ContentEntityDeleteForm; |
9 | use Drupal\Core\Entity\ContentEntityForm; |
10 | use Drupal\Core\Entity\EntityChangedTrait; |
11 | use Drupal\Core\Entity\EntityPublishedTrait; |
12 | use Drupal\Core\Entity\EntityTypeInterface; |
13 | use Drupal\Core\Entity\RevisionableContentEntityBase; |
14 | use Drupal\Core\Entity\RevisionLogEntityTrait; |
15 | use Drupal\Core\Entity\Routing\AdminHtmlRouteProvider; |
16 | use Drupal\Core\Field\BaseFieldDefinition; |
17 | use Drupal\Core\StringTranslation\TranslatableMarkup; |
18 | use Drupal\crm\ContactDetailAccessControlHandler; |
19 | use Drupal\crm\ContactDetailListBuilder; |
20 | use Drupal\crm\CrmContactDetailInterface; |
21 | use Drupal\views\EntityViewsData; |
22 | |
23 | /** |
24 | * CRM contact detail. |
25 | * |
26 | * A contact can have many details: email, address, phone number, etc. |
27 | */ |
28 | #[ContentEntityType( |
29 | id: 'crm_contact_detail', |
30 | label: new TranslatableMarkup('CRM Contact Detail'), |
31 | label_collection: new TranslatableMarkup('CRM Contact Details'), |
32 | label_singular: new TranslatableMarkup('crm contact detail'), |
33 | label_plural: new TranslatableMarkup('crm contact details'), |
34 | label_count: [ |
35 | 'singular' => '@count crm contact detail', |
36 | 'plural' => '@count crm contact details', |
37 | ], |
38 | bundle_label: new TranslatableMarkup('CRM Contact Detail type'), |
39 | handlers: [ |
40 | 'access' => ContactDetailAccessControlHandler::class, |
41 | 'list_builder' => ContactDetailListBuilder::class, |
42 | 'views_data' => EntityViewsData::class, |
43 | 'form' => [ |
44 | 'default' => ContentEntityForm::class, |
45 | 'delete' => ContentEntityDeleteForm::class, |
46 | ], |
47 | 'route_provider' => [ |
48 | 'html' => AdminHtmlRouteProvider::class, |
49 | ], |
50 | ], |
51 | base_table: 'crm_contact_detail', |
52 | revision_table: 'crm_contact_detail_revision', |
53 | show_revision_ui: TRUE, |
54 | admin_permission: 'administer crm', |
55 | entity_keys: [ |
56 | 'id' => 'id', |
57 | 'revision' => 'revision_id', |
58 | 'bundle' => 'bundle', |
59 | 'label' => 'id', |
60 | 'uuid' => 'uuid', |
61 | 'status' => 'status', |
62 | 'published' => 'status', |
63 | ], |
64 | revision_metadata_keys: [ |
65 | 'revision_user' => 'revision_uid', |
66 | 'revision_created' => 'revision_timestamp', |
67 | 'revision_log_message' => 'revision_log', |
68 | ], |
69 | bundle_entity_type: 'crm_contact_detail_type', |
70 | field_ui_base_route: 'entity.crm_contact_detail_type.edit_form', |
71 | translatable: FALSE, |
72 | links: [ |
73 | 'collection' => '/admin/content/crm/detail', |
74 | ], |
75 | )] |
76 | class ContactDetail extends RevisionableContentEntityBase implements CrmContactDetailInterface { |
77 | |
78 | use EntityChangedTrait; |
79 | use EntityPublishedTrait; |
80 | use RevisionLogEntityTrait; |
81 | |
82 | /** |
83 | * {@inheritdoc} |
84 | */ |
85 | public static function baseFieldDefinitions(EntityTypeInterface $entity_type): array { |
86 | |
87 | $fields = parent::baseFieldDefinitions($entity_type); |
88 | |
89 | $fields['type'] = BaseFieldDefinition::create('entity_reference') |
90 | ->setRevisionable(TRUE) |
91 | ->setLabel(t('Type')) |
92 | ->setSetting('target_type', 'crm_detail_type') |
93 | ->setDisplayOptions('form', [ |
94 | 'type' => 'options_select', |
95 | 'weight' => -10, |
96 | ]) |
97 | ->setDisplayConfigurable('form', TRUE) |
98 | ->setDisplayOptions('view', [ |
99 | 'label' => 'above', |
100 | 'type' => 'entity_reference_label', |
101 | 'weight' => 10, |
102 | ]) |
103 | ->setDisplayConfigurable('view', TRUE); |
104 | |
105 | $fields['status'] = BaseFieldDefinition::create('boolean') |
106 | ->setRevisionable(TRUE) |
107 | ->setLabel(t('Status')) |
108 | ->setDefaultValue(TRUE) |
109 | ->setSetting('on_label', 'Enabled') |
110 | ->setDisplayOptions('form', [ |
111 | 'type' => 'boolean_checkbox', |
112 | 'settings' => [ |
113 | 'display_label' => FALSE, |
114 | ], |
115 | 'weight' => 0, |
116 | ]) |
117 | ->setDisplayConfigurable('form', TRUE) |
118 | ->setDisplayOptions('view', [ |
119 | 'type' => 'boolean', |
120 | 'label' => 'above', |
121 | 'weight' => 0, |
122 | 'settings' => [ |
123 | 'format' => 'enabled-disabled', |
124 | ], |
125 | ]) |
126 | ->setDisplayConfigurable('view', TRUE); |
127 | |
128 | $fields['crm_contact'] = BaseFieldDefinition::create('entity_reference') |
129 | ->setLabel(t('CRM Contact')) |
130 | ->setDescription(t('The CRM Contact that this detail belongs to.')) |
131 | ->setSetting('target_type', 'crm_contact'); |
132 | |
133 | $fields['created'] = BaseFieldDefinition::create('created') |
134 | ->setLabel(t('Created on')) |
135 | ->setDescription(t('The time that the crm contact detail was created.')) |
136 | ->setDisplayOptions('view', [ |
137 | 'label' => 'above', |
138 | 'type' => 'timestamp', |
139 | 'weight' => 20, |
140 | ]) |
141 | ->setDisplayConfigurable('form', TRUE) |
142 | ->setDisplayOptions('form', [ |
143 | 'type' => 'datetime_timestamp', |
144 | 'weight' => 20, |
145 | ]) |
146 | ->setDisplayConfigurable('view', TRUE); |
147 | |
148 | $fields['changed'] = BaseFieldDefinition::create('changed') |
149 | ->setLabel(t('Changed')) |
150 | ->setDescription(t('The time that the crm contact detail was last edited.')); |
151 | |
152 | return $fields; |
153 | } |
154 | |
155 | /** |
156 | * {@inheritdoc} |
157 | */ |
158 | public function label() { |
159 | $type = ''; |
160 | |
161 | if ($this->get('type')->isEmpty()) { |
162 | return $type; |
163 | } |
164 | |
165 | /** @var \Drupal\Core\Entity\Plugin\DataType\EntityReference $detail_type_entity */ |
166 | $detail_type_entity_reference = $this->get('type') |
167 | ->first() |
168 | ->get('entity'); |
169 | |
170 | /** @var \Drupal\crm_field\CrmDetailTypeInterface $detail_type_entity */ |
171 | $detail_type_entity = $detail_type_entity_reference->getTarget() |
172 | ?->getValue(); |
173 | $type = $detail_type_entity?->label(); |
174 | |
175 | return $type; |
176 | } |
177 | |
178 | } |