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