Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
89.89% |
80 / 89 |
|
66.67% |
2 / 3 |
CRAP | |
0.00% |
0 / 1 |
ContactDetail | |
89.89% |
80 / 89 |
|
66.67% |
2 / 3 |
5.03 | |
0.00% |
0 / 1 |
preSave | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
baseFieldDefinitions | |
100.00% |
77 / 77 |
|
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\EntityChangedTrait; |
8 | use Drupal\Core\Entity\EntityStorageInterface; |
9 | use Drupal\Core\Entity\EntityTypeInterface; |
10 | use Drupal\Core\Entity\RevisionableContentEntityBase; |
11 | use Drupal\Core\Field\BaseFieldDefinition; |
12 | use Drupal\crm\CrmContactDetailInterface; |
13 | use Drupal\user\EntityOwnerTrait; |
14 | |
15 | /** |
16 | * Defines the CRM contact detail entity class. |
17 | * |
18 | * @ContentEntityType( |
19 | * id = "crm_contact_detail", |
20 | * label = @Translation("CRM Contact Detail"), |
21 | * label_collection = @Translation("CRM Contact Details"), |
22 | * label_singular = @Translation("contact detail"), |
23 | * label_plural = @Translation("contact details"), |
24 | * label_count = @PluralTranslation( |
25 | * singular = "@count contact details", |
26 | * plural = "@count contact details", |
27 | * ), |
28 | * bundle_label = @Translation("CRM Contact Detail type"), |
29 | * handlers = { |
30 | * "list_builder" = "Drupal\crm\ContactDetailListBuilder", |
31 | * "views_data" = "Drupal\views\EntityViewsData", |
32 | * "form" = { |
33 | * "add" = "Drupal\Drupal\Core\Entity\ContentEntityForm", |
34 | * "edit" = "Drupal\Drupal\Core\Entity\ContentEntityForm", |
35 | * "delete" = "Drupal\Core\Entity\ContentEntityDeleteForm", |
36 | * }, |
37 | * }, |
38 | * base_table = "crm_contact_detail", |
39 | * revision_table = "crm_contact_detail_revision", |
40 | * show_revision_ui = TRUE, |
41 | * admin_permission = "administer crm contact detail types", |
42 | * entity_keys = { |
43 | * "id" = "id", |
44 | * "revision" = "revision_id", |
45 | * "bundle" = "bundle", |
46 | * "label" = "id", |
47 | * "uuid" = "uuid", |
48 | * "status" = "status", |
49 | * "published" = "status", |
50 | * "owner" = "uid", |
51 | * "uid" = "uid", |
52 | * }, |
53 | * revision_metadata_keys = { |
54 | * "revision_user" = "revision_uid", |
55 | * "revision_created" = "revision_timestamp", |
56 | * "revision_log_message" = "revision_log", |
57 | * }, |
58 | * bundle_entity_type = "crm_contact_detail_type", |
59 | * field_ui_base_route = "entity.crm_contact_detail_type.edit_form", |
60 | * ) |
61 | */ |
62 | class ContactDetail extends RevisionableContentEntityBase implements CrmContactDetailInterface { |
63 | |
64 | use EntityChangedTrait; |
65 | use EntityOwnerTrait; |
66 | |
67 | /** |
68 | * {@inheritdoc} |
69 | */ |
70 | public function preSave(EntityStorageInterface $storage): void { |
71 | parent::preSave($storage); |
72 | if (!$this->getOwnerId()) { |
73 | // If no owner has been set explicitly, make the anonymous user the owner. |
74 | $this->setOwnerId(0); |
75 | } |
76 | } |
77 | |
78 | /** |
79 | * {@inheritdoc} |
80 | */ |
81 | public static function baseFieldDefinitions(EntityTypeInterface $entity_type): array { |
82 | |
83 | $fields = parent::baseFieldDefinitions($entity_type); |
84 | |
85 | $fields['uid'] = BaseFieldDefinition::create('entity_reference') |
86 | ->setRevisionable(TRUE) |
87 | ->setLabel(t('Author')) |
88 | ->setSetting('target_type', 'user') |
89 | ->setDefaultValueCallback(self::class . '::getDefaultEntityOwner') |
90 | ->setDisplayOptions('form', [ |
91 | 'type' => 'entity_reference_autocomplete', |
92 | 'settings' => [ |
93 | 'match_operator' => 'CONTAINS', |
94 | 'size' => 60, |
95 | 'placeholder' => '', |
96 | ], |
97 | 'weight' => 15, |
98 | ]) |
99 | ->setDisplayConfigurable('form', TRUE) |
100 | ->setDisplayOptions('view', [ |
101 | 'label' => 'above', |
102 | 'type' => 'author', |
103 | 'weight' => 15, |
104 | ]) |
105 | ->setDisplayConfigurable('view', TRUE); |
106 | |
107 | $fields['type'] = BaseFieldDefinition::create('entity_reference') |
108 | ->setRevisionable(TRUE) |
109 | ->setLabel(t('Type')) |
110 | ->setSetting('target_type', 'crm_detail_type') |
111 | ->setDisplayOptions('form', [ |
112 | 'type' => 'options_select', |
113 | 'weight' => -10, |
114 | ]) |
115 | ->setDisplayConfigurable('form', TRUE) |
116 | ->setDisplayOptions('view', [ |
117 | 'label' => 'above', |
118 | 'type' => 'entity_reference_label', |
119 | 'weight' => 10, |
120 | ]) |
121 | ->setDisplayConfigurable('view', TRUE); |
122 | |
123 | $fields['status'] = BaseFieldDefinition::create('boolean') |
124 | ->setRevisionable(TRUE) |
125 | ->setLabel(t('Status')) |
126 | ->setDefaultValue(TRUE) |
127 | ->setSetting('on_label', 'Enabled') |
128 | ->setDisplayOptions('form', [ |
129 | 'type' => 'boolean_checkbox', |
130 | 'settings' => [ |
131 | 'display_label' => FALSE, |
132 | ], |
133 | 'weight' => 0, |
134 | ]) |
135 | ->setDisplayConfigurable('form', TRUE) |
136 | ->setDisplayOptions('view', [ |
137 | 'type' => 'boolean', |
138 | 'label' => 'above', |
139 | 'weight' => 0, |
140 | 'settings' => [ |
141 | 'format' => 'enabled-disabled', |
142 | ], |
143 | ]) |
144 | ->setDisplayConfigurable('view', TRUE); |
145 | |
146 | $fields['created'] = BaseFieldDefinition::create('created') |
147 | ->setLabel(t('Authored on')) |
148 | ->setDescription(t('The time that the crm contact detail was created.')) |
149 | ->setDisplayOptions('view', [ |
150 | 'label' => 'above', |
151 | 'type' => 'timestamp', |
152 | 'weight' => 20, |
153 | ]) |
154 | ->setDisplayConfigurable('form', TRUE) |
155 | ->setDisplayOptions('form', [ |
156 | 'type' => 'datetime_timestamp', |
157 | 'weight' => 20, |
158 | ]) |
159 | ->setDisplayConfigurable('view', TRUE); |
160 | |
161 | $fields['changed'] = BaseFieldDefinition::create('changed') |
162 | ->setLabel(t('Changed')) |
163 | ->setDescription(t('The time that the crm contact detail was last edited.')); |
164 | |
165 | return $fields; |
166 | } |
167 | |
168 | /** |
169 | * {@inheritdoc} |
170 | */ |
171 | public function label() { |
172 | $type = ''; |
173 | |
174 | if ($this->get('type')->isEmpty()) { |
175 | return $type; |
176 | } |
177 | |
178 | /** @var \Drupal\Core\Entity\Plugin\DataType\EntityReference $detail_type_entity */ |
179 | $detail_type_entity_reference = $this->get('type') |
180 | ->first() |
181 | ->get('entity'); |
182 | |
183 | /** @var \Drupal\crm_field\CrmDetailTypeInterface $detail_type_entity */ |
184 | $detail_type_entity = $detail_type_entity_reference->getTarget() |
185 | ?->getValue(); |
186 | $type = $detail_type_entity?->label(); |
187 | |
188 | return $type; |
189 | } |
190 | |
191 | } |