Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
95.87% |
116 / 121 |
|
50.00% |
1 / 2 |
CRAP | |
0.00% |
0 / 1 |
Relationship | |
95.87% |
116 / 121 |
|
50.00% |
1 / 2 |
2 | |
0.00% |
0 / 1 |
baseFieldDefinitions | |
100.00% |
116 / 116 |
|
100.00% |
1 / 1 |
1 | |||
label | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
2 |
1 | <?php |
2 | |
3 | namespace Drupal\crm\Entity; |
4 | |
5 | use Drupal\Core\Entity\EntityChangedTrait; |
6 | use Drupal\Core\Entity\EntityPublishedTrait; |
7 | use Drupal\Core\Entity\EntityTypeInterface; |
8 | use Drupal\Core\Entity\RevisionableContentEntityBase; |
9 | use Drupal\Core\Field\BaseFieldDefinition; |
10 | use Drupal\crm\CrmRelationshipInterface; |
11 | |
12 | /** |
13 | * Defines the crm relationship entity class. |
14 | * |
15 | * @ContentEntityType( |
16 | * id = "crm_relationship", |
17 | * label = @Translation("CRM Relationship"), |
18 | * label_collection = @Translation("CRM Relationships"), |
19 | * label_singular = @Translation("crm relationship"), |
20 | * label_plural = @Translation("crm relationships"), |
21 | * label_count = @PluralTranslation( |
22 | * singular = "@count crm relationships", |
23 | * plural = "@count crm relationships", |
24 | * ), |
25 | * bundle_label = @Translation("CRM Relationship type"), |
26 | * handlers = { |
27 | * "list_builder" = "Drupal\crm\RelationshipListBuilder", |
28 | * "views_data" = "Drupal\views\EntityViewsData", |
29 | * "access" = "Drupal\crm\RelationshipAccessControlHandler", |
30 | * "form" = { |
31 | * "add" = "Drupal\crm\Form\RelationshipForm", |
32 | * "edit" = "Drupal\crm\Form\RelationshipForm", |
33 | * "delete" = "Drupal\Core\Entity\ContentEntityDeleteForm", |
34 | * }, |
35 | * "route_provider" = { |
36 | * "html" = "Drupal\Core\Entity\Routing\AdminHtmlRouteProvider", |
37 | * } |
38 | * }, |
39 | * base_table = "crm_relationship", |
40 | * revision_table = "crm_relationship_revision", |
41 | * show_revision_ui = TRUE, |
42 | * translatable = FALSE, |
43 | * admin_permission = "administer crm relationship types", |
44 | * entity_keys = { |
45 | * "id" = "id", |
46 | * "revision" = "revision_id", |
47 | * "bundle" = "bundle", |
48 | * "uuid" = "uuid", |
49 | * "status" = "status", |
50 | * "published" = "status", |
51 | * }, |
52 | * revision_metadata_keys = { |
53 | * "revision_user" = "revision_uid", |
54 | * "revision_created" = "revision_timestamp", |
55 | * "revision_log_message" = "revision_log", |
56 | * }, |
57 | * links = { |
58 | * "collection" = "/admin/content/crm/relationship", |
59 | * "add-form" = "/crm/relationship/add/{crm_relationship_type}", |
60 | * "add-page" = "/crm/relationship/add", |
61 | * "canonical" = "/crm/relationship/{crm_relationship}", |
62 | * "edit-form" = "/crm/relationship/{crm_relationship}/edit", |
63 | * "delete-form" = "/crm/relationship/{crm_relationship}/delete", |
64 | * }, |
65 | * bundle_entity_type = "crm_relationship_type", |
66 | * field_ui_base_route = "entity.crm_relationship_type.edit_form", |
67 | * constraints = { |
68 | * "DifferentContacts" = {} |
69 | * }, |
70 | * ) |
71 | */ |
72 | class Relationship extends RevisionableContentEntityBase implements CrmRelationshipInterface { |
73 | |
74 | use EntityChangedTrait; |
75 | use EntityPublishedTrait; |
76 | |
77 | /** |
78 | * {@inheritdoc} |
79 | */ |
80 | public static function baseFieldDefinitions(EntityTypeInterface $entity_type) { |
81 | |
82 | $fields = parent::baseFieldDefinitions($entity_type); |
83 | // Add the published field. |
84 | $fields += static::publishedBaseFieldDefinitions($entity_type); |
85 | |
86 | $fields['contact_a'] = BaseFieldDefinition::create('entity_reference') |
87 | ->setLabel('Contact A') |
88 | ->setSetting('target_type', 'crm_contact') |
89 | ->setDisplayConfigurable('form', TRUE) |
90 | ->setDisplayOptions('form', [ |
91 | 'type' => 'entity_reference_autocomplete', |
92 | 'weight' => 0, |
93 | ]) |
94 | ->setDisplayConfigurable('view', TRUE) |
95 | ->setDisplayOptions('view', [ |
96 | 'type' => 'entity_reference_label', |
97 | 'label' => 'above', |
98 | 'weight' => 0, |
99 | ]) |
100 | ->setRequired(TRUE); |
101 | |
102 | $fields['contact_b'] = BaseFieldDefinition::create('entity_reference') |
103 | ->setLabel('Contact B') |
104 | ->setSetting('target_type', 'crm_contact') |
105 | ->setDisplayConfigurable('form', TRUE) |
106 | ->setDisplayOptions('form', [ |
107 | 'type' => 'entity_reference_autocomplete', |
108 | 'weight' => 0, |
109 | ]) |
110 | ->setDisplayConfigurable('view', TRUE) |
111 | ->setDisplayOptions('view', [ |
112 | 'type' => 'entity_reference_label', |
113 | 'label' => 'above', |
114 | 'weight' => 0, |
115 | ]) |
116 | ->setRequired(TRUE); |
117 | |
118 | $fields['status'] = BaseFieldDefinition::create('boolean') |
119 | ->setRevisionable(TRUE) |
120 | ->setLabel(t('Status')) |
121 | ->setDefaultValue(TRUE) |
122 | ->setSetting('on_label', 'Status') |
123 | ->setDisplayOptions('form', [ |
124 | 'type' => 'boolean_checkbox', |
125 | 'settings' => [ |
126 | 'display_label' => FALSE, |
127 | ], |
128 | 'weight' => 0, |
129 | ]) |
130 | ->setDisplayConfigurable('form', TRUE) |
131 | ->setDisplayOptions('view', [ |
132 | 'type' => 'boolean', |
133 | 'label' => 'above', |
134 | 'weight' => 0, |
135 | 'settings' => [ |
136 | 'format' => 'enabled-disabled', |
137 | ], |
138 | ]) |
139 | ->setDisplayConfigurable('view', TRUE); |
140 | |
141 | $fields['start'] = BaseFieldDefinition::create('datetime') |
142 | ->setLabel(t('Start Date')) |
143 | ->setDescription(t('When the relationship started.')) |
144 | ->setRevisionable(TRUE) |
145 | ->setSettings([ |
146 | 'datetime_type' => 'date', |
147 | ]) |
148 | ->setDefaultValue('') |
149 | ->setDisplayOptions('view', [ |
150 | 'label' => 'above', |
151 | 'type' => 'datetime_default', |
152 | 'settings' => [ |
153 | 'format_type' => 'medium', |
154 | ], |
155 | 'weight' => -9, |
156 | ]) |
157 | ->setDisplayOptions('form', [ |
158 | 'type' => 'datetime_default', |
159 | 'weight' => -9, |
160 | ]) |
161 | ->setDisplayConfigurable('form', TRUE) |
162 | ->setDisplayConfigurable('view', TRUE); |
163 | |
164 | $fields['end'] = BaseFieldDefinition::create('datetime') |
165 | ->setLabel(t('End Date')) |
166 | ->setDescription(t('When the relationship ends.')) |
167 | ->setRevisionable(TRUE) |
168 | ->setSettings([ |
169 | 'datetime_type' => 'date', |
170 | ]) |
171 | ->setDefaultValue('') |
172 | ->setDisplayOptions('view', [ |
173 | 'label' => 'above', |
174 | 'type' => 'datetime_default', |
175 | 'settings' => [ |
176 | 'format_type' => 'medium', |
177 | ], |
178 | 'weight' => -9, |
179 | ]) |
180 | ->setDisplayOptions('form', [ |
181 | 'type' => 'datetime_default', |
182 | 'weight' => -9, |
183 | ]) |
184 | ->setDisplayConfigurable('form', TRUE) |
185 | ->setDisplayConfigurable('view', TRUE); |
186 | $fields['created'] = BaseFieldDefinition::create('created') |
187 | ->setLabel(t('Created on')) |
188 | ->setDescription(t('The time that the crm relationship was created.')) |
189 | ->setDisplayOptions('view', [ |
190 | 'label' => 'above', |
191 | 'type' => 'timestamp', |
192 | 'weight' => 20, |
193 | ]) |
194 | ->setDisplayConfigurable('form', TRUE) |
195 | ->setDisplayOptions('form', [ |
196 | 'type' => 'datetime_timestamp', |
197 | 'weight' => 20, |
198 | ]) |
199 | ->setDisplayConfigurable('view', TRUE); |
200 | |
201 | $fields['changed'] = BaseFieldDefinition::create('changed') |
202 | ->setLabel(t('Changed')) |
203 | ->setDescription(t('The time that the crm relationship was last edited.')); |
204 | |
205 | return $fields; |
206 | } |
207 | |
208 | /** |
209 | * {@inheritdoc} |
210 | */ |
211 | public function label() { |
212 | // Get the entity bundle entity. |
213 | $bundle = $this->bundle->entity; |
214 | $label = $bundle->get('label'); |
215 | $contact_a = $this->get('contact_a')->entity->label(); |
216 | $contact_b = $this->get('contact_b')->entity->label(); |
217 | |
218 | return "$label ($contact_a<=>$contact_b)"; |
219 | } |
220 | |
221 | } |