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