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