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