Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
96.73% |
148 / 153 |
|
50.00% |
1 / 2 |
CRAP | |
0.00% |
0 / 1 |
| Relationship | |
96.73% |
148 / 153 |
|
50.00% |
1 / 2 |
2 | |
0.00% |
0 / 1 |
| baseFieldDefinitions | |
100.00% |
148 / 148 |
|
100.00% |
1 / 1 |
1 | |||
| label | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
2 | |||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace Drupal\crm\Entity; |
| 6 | |
| 7 | use Drupal\Core\Entity\Attribute\ContentEntityType; |
| 8 | use Drupal\Core\Entity\ContentEntityDeleteForm; |
| 9 | use Drupal\Core\Entity\EntityChangedTrait; |
| 10 | use Drupal\Core\Entity\EntityTypeInterface; |
| 11 | use Drupal\Core\Entity\RevisionableContentEntityBase; |
| 12 | use Drupal\Core\Entity\RevisionLogEntityTrait; |
| 13 | use Drupal\Core\Entity\Routing\AdminHtmlRouteProvider; |
| 14 | use Drupal\Core\Field\BaseFieldDefinition; |
| 15 | use Drupal\Core\StringTranslation\TranslatableMarkup; |
| 16 | use Drupal\crm\CrmRelationshipInterface; |
| 17 | use Drupal\crm\Field\AgeFieldItemList; |
| 18 | use Drupal\crm\Field\RelationshipContactsItemList; |
| 19 | use Drupal\crm\Form\RelationshipForm; |
| 20 | use Drupal\crm\RelationshipAccessControlHandler; |
| 21 | use Drupal\crm\RelationshipListBuilder; |
| 22 | use Drupal\views\EntityViewsData; |
| 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 | ], |
| 63 | revision_metadata_keys: [ |
| 64 | 'revision_user' => 'revision_uid', |
| 65 | 'revision_created' => 'revision_timestamp', |
| 66 | 'revision_log_message' => 'revision_log', |
| 67 | ], |
| 68 | bundle_entity_type: 'crm_relationship_type', |
| 69 | field_ui_base_route: 'entity.crm_relationship_type.edit_form', |
| 70 | translatable: FALSE, |
| 71 | constraints: [ |
| 72 | 'RelationshipContacts' => [], |
| 73 | 'RelationshipLimit' => [], |
| 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 | )] |
| 84 | class Relationship extends RevisionableContentEntityBase implements CrmRelationshipInterface { |
| 85 | |
| 86 | use EntityChangedTrait; |
| 87 | use RevisionLogEntityTrait; |
| 88 | |
| 89 | /** |
| 90 | * {@inheritdoc} |
| 91 | */ |
| 92 | public static function baseFieldDefinitions(EntityTypeInterface $entity_type) { |
| 93 | |
| 94 | $fields = parent::baseFieldDefinitions($entity_type); |
| 95 | $fields['contacts'] = BaseFieldDefinition::create('entity_reference') |
| 96 | ->setLabel('Contacts') |
| 97 | ->setSetting('target_type', 'crm_contact') |
| 98 | ->setDisplayConfigurable('form', FALSE) |
| 99 | ->setDisplayConfigurable('view', FALSE) |
| 100 | ->setCardinality(2) |
| 101 | ->setRequired(TRUE) |
| 102 | ->setRevisionable(TRUE); |
| 103 | |
| 104 | $fields['contact_a'] = BaseFieldDefinition::create('entity_reference') |
| 105 | ->setLabel('Contact A') |
| 106 | ->setDescription(t('The first contact in the relationship.')) |
| 107 | ->setComputed(TRUE) |
| 108 | ->setClass(RelationshipContactsItemList::class) |
| 109 | ->setSetting('target_type', 'crm_contact') |
| 110 | ->setDisplayConfigurable('form', TRUE) |
| 111 | ->setDisplayOptions('form', [ |
| 112 | 'type' => 'entity_reference_autocomplete', |
| 113 | 'weight' => 0, |
| 114 | ]) |
| 115 | ->setDisplayConfigurable('view', TRUE) |
| 116 | ->setDisplayOptions('view', [ |
| 117 | 'type' => 'entity_reference_label', |
| 118 | 'label' => 'above', |
| 119 | 'weight' => 0, |
| 120 | ]) |
| 121 | ->setRequired(TRUE); |
| 122 | |
| 123 | $fields['contact_b'] = BaseFieldDefinition::create('entity_reference') |
| 124 | ->setLabel('Contact B') |
| 125 | ->setDescription(t('The second contact in the relationship.')) |
| 126 | ->setComputed(TRUE) |
| 127 | ->setClass(RelationshipContactsItemList::class) |
| 128 | ->setSetting('target_type', 'crm_contact') |
| 129 | ->setDisplayConfigurable('form', TRUE) |
| 130 | ->setDisplayOptions('form', [ |
| 131 | 'type' => 'entity_reference_autocomplete', |
| 132 | 'weight' => 0, |
| 133 | ]) |
| 134 | ->setDisplayConfigurable('view', TRUE) |
| 135 | ->setDisplayOptions('view', [ |
| 136 | 'type' => 'entity_reference_label', |
| 137 | 'label' => 'above', |
| 138 | 'weight' => 0, |
| 139 | ]) |
| 140 | ->setRequired(TRUE); |
| 141 | |
| 142 | $fields['status'] = BaseFieldDefinition::create('boolean') |
| 143 | ->setRevisionable(TRUE) |
| 144 | ->setLabel(t('Status')) |
| 145 | ->setDefaultValue(TRUE) |
| 146 | ->setSetting('on_label', 'Status') |
| 147 | ->setDisplayOptions('form', [ |
| 148 | 'type' => 'boolean_checkbox', |
| 149 | 'settings' => [ |
| 150 | 'display_label' => FALSE, |
| 151 | ], |
| 152 | 'weight' => 0, |
| 153 | ]) |
| 154 | ->setDisplayConfigurable('form', TRUE) |
| 155 | ->setDisplayOptions('view', [ |
| 156 | 'type' => 'boolean', |
| 157 | 'label' => 'above', |
| 158 | 'weight' => 0, |
| 159 | 'settings' => [ |
| 160 | 'format' => 'enabled-disabled', |
| 161 | ], |
| 162 | ]) |
| 163 | ->setDisplayConfigurable('view', TRUE); |
| 164 | |
| 165 | $fields['start_date'] = BaseFieldDefinition::create('datetime') |
| 166 | ->setLabel(t('Start Date')) |
| 167 | ->setDescription(t('When the relationship started.')) |
| 168 | ->setRevisionable(TRUE) |
| 169 | ->setSettings([ |
| 170 | 'datetime_type' => 'date', |
| 171 | ]) |
| 172 | ->setDefaultValue('') |
| 173 | ->setDisplayOptions('view', [ |
| 174 | 'label' => 'above', |
| 175 | 'type' => 'datetime_default', |
| 176 | 'settings' => [ |
| 177 | 'format_type' => 'medium', |
| 178 | ], |
| 179 | 'weight' => -9, |
| 180 | ]) |
| 181 | ->setDisplayOptions('form', [ |
| 182 | 'type' => 'datetime_default', |
| 183 | 'weight' => -9, |
| 184 | ]) |
| 185 | ->setDisplayConfigurable('form', TRUE) |
| 186 | ->setDisplayConfigurable('view', TRUE) |
| 187 | ->setRevisionable(TRUE); |
| 188 | |
| 189 | $fields['end_date'] = BaseFieldDefinition::create('datetime') |
| 190 | ->setLabel(t('End Date')) |
| 191 | ->setDescription(t('When the relationship ends.')) |
| 192 | ->setRevisionable(TRUE) |
| 193 | ->setSettings([ |
| 194 | 'datetime_type' => 'date', |
| 195 | ]) |
| 196 | ->setDefaultValue('') |
| 197 | ->setDisplayOptions('view', [ |
| 198 | 'label' => 'above', |
| 199 | 'type' => 'datetime_default', |
| 200 | 'settings' => [ |
| 201 | 'format_type' => 'medium', |
| 202 | ], |
| 203 | 'weight' => -9, |
| 204 | ]) |
| 205 | ->setDisplayOptions('form', [ |
| 206 | 'type' => 'datetime_default', |
| 207 | 'weight' => -9, |
| 208 | ]) |
| 209 | ->setDisplayConfigurable('form', TRUE) |
| 210 | ->setDisplayConfigurable('view', TRUE) |
| 211 | ->setRevisionable(TRUE); |
| 212 | |
| 213 | $fields['age'] = BaseFieldDefinition::create('integer') |
| 214 | ->setLabel(t('Age')) |
| 215 | ->setDescription(t('The age of the relationship.')) |
| 216 | ->setComputed(TRUE) |
| 217 | ->setClass(AgeFieldItemList::class) |
| 218 | ->setDisplayConfigurable('form', FALSE) |
| 219 | ->setDisplayConfigurable('view', TRUE) |
| 220 | ->setDisplayOptions('view', [ |
| 221 | 'label' => 'above', |
| 222 | 'type' => 'age_formatter', |
| 223 | 'settings' => [ |
| 224 | 'granularity' => 'years', |
| 225 | ], |
| 226 | 'weight' => -9, |
| 227 | ]); |
| 228 | |
| 229 | $fields['created'] = BaseFieldDefinition::create('created') |
| 230 | ->setLabel(t('Created on')) |
| 231 | ->setDescription(t('The time that the crm relationship was created.')) |
| 232 | ->setDisplayOptions('view', [ |
| 233 | 'label' => 'above', |
| 234 | 'type' => 'timestamp', |
| 235 | 'weight' => 20, |
| 236 | ]) |
| 237 | ->setDisplayConfigurable('form', TRUE) |
| 238 | ->setDisplayOptions('form', [ |
| 239 | 'type' => 'datetime_timestamp', |
| 240 | 'weight' => 20, |
| 241 | ]) |
| 242 | ->setDisplayConfigurable('view', TRUE) |
| 243 | ->setRevisionable(TRUE); |
| 244 | |
| 245 | $fields['changed'] = BaseFieldDefinition::create('changed') |
| 246 | ->setLabel(t('Changed')) |
| 247 | ->setDescription(t('The time that the crm relationship was last edited.')) |
| 248 | ->setRevisionable(TRUE); |
| 249 | |
| 250 | return $fields; |
| 251 | } |
| 252 | |
| 253 | /** |
| 254 | * {@inheritdoc} |
| 255 | */ |
| 256 | public function label() { |
| 257 | // Get the entity bundle entity. |
| 258 | $bundle = $this->get('bundle')->entity; |
| 259 | $label = $bundle->get('label'); |
| 260 | $contact_a = $this->get('contact_a')->entity->label(); |
| 261 | $contact_b = $this->get('contact_b')->entity->label(); |
| 262 | |
| 263 | return "$label ($contact_a<=>$contact_b)"; |
| 264 | } |
| 265 | |
| 266 | } |