Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
60 / 60 |
|
100.00% |
1 / 1 |
CRAP | |
100.00% |
1 / 1 |
| ContactDetail | |
100.00% |
60 / 60 |
|
100.00% |
1 / 1 |
1 | |
100.00% |
1 / 1 |
| baseFieldDefinitions | |
100.00% |
60 / 60 |
|
100.00% |
1 / 1 |
1 | |||
| 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\ContentEntityForm; |
| 10 | use Drupal\Core\Entity\EntityChangedTrait; |
| 11 | use Drupal\Core\Entity\EntityTypeInterface; |
| 12 | use Drupal\Core\Entity\RevisionableContentEntityBase; |
| 13 | use Drupal\Core\Entity\RevisionLogEntityTrait; |
| 14 | use Drupal\Core\Entity\Routing\AdminHtmlRouteProvider; |
| 15 | use Drupal\Core\Field\BaseFieldDefinition; |
| 16 | use Drupal\Core\StringTranslation\TranslatableMarkup; |
| 17 | use Drupal\crm\ContactDetailAccessControlHandler; |
| 18 | use Drupal\crm\ContactDetailListBuilder; |
| 19 | use Drupal\crm\CrmContactDetailInterface; |
| 20 | use Drupal\views\EntityViewsData; |
| 21 | |
| 22 | /** |
| 23 | * CRM contact detail. |
| 24 | * |
| 25 | * A contact can have many details: email, address, phone number, etc. |
| 26 | */ |
| 27 | #[ContentEntityType( |
| 28 | id: 'crm_contact_detail', |
| 29 | label: new TranslatableMarkup('CRM Contact Detail'), |
| 30 | label_collection: new TranslatableMarkup('CRM Contact Details'), |
| 31 | label_singular: new TranslatableMarkup('crm contact detail'), |
| 32 | label_plural: new TranslatableMarkup('crm contact details'), |
| 33 | label_count: [ |
| 34 | 'singular' => '@count crm contact detail', |
| 35 | 'plural' => '@count crm contact details', |
| 36 | ], |
| 37 | bundle_label: new TranslatableMarkup('CRM Contact Detail type'), |
| 38 | handlers: [ |
| 39 | 'access' => ContactDetailAccessControlHandler::class, |
| 40 | 'list_builder' => ContactDetailListBuilder::class, |
| 41 | 'views_data' => EntityViewsData::class, |
| 42 | 'form' => [ |
| 43 | 'default' => ContentEntityForm::class, |
| 44 | 'delete' => ContentEntityDeleteForm::class, |
| 45 | ], |
| 46 | 'route_provider' => [ |
| 47 | 'html' => AdminHtmlRouteProvider::class, |
| 48 | ], |
| 49 | ], |
| 50 | base_table: 'crm_contact_detail', |
| 51 | revision_table: 'crm_contact_detail_revision', |
| 52 | show_revision_ui: TRUE, |
| 53 | admin_permission: 'administer crm', |
| 54 | entity_keys: [ |
| 55 | 'id' => 'id', |
| 56 | 'revision' => 'revision_id', |
| 57 | 'bundle' => 'bundle', |
| 58 | 'label' => 'id', |
| 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_contact_detail_type', |
| 68 | field_ui_base_route: 'entity.crm_contact_detail_type.edit_form', |
| 69 | translatable: FALSE, |
| 70 | links: [ |
| 71 | 'collection' => '/admin/content/crm/detail', |
| 72 | ], |
| 73 | )] |
| 74 | class ContactDetail extends RevisionableContentEntityBase implements CrmContactDetailInterface { |
| 75 | |
| 76 | use EntityChangedTrait; |
| 77 | use RevisionLogEntityTrait; |
| 78 | |
| 79 | /** |
| 80 | * {@inheritdoc} |
| 81 | */ |
| 82 | public static function baseFieldDefinitions(EntityTypeInterface $entity_type): array { |
| 83 | |
| 84 | $fields = parent::baseFieldDefinitions($entity_type); |
| 85 | |
| 86 | $fields['type'] = BaseFieldDefinition::create('entity_reference') |
| 87 | ->setRevisionable(TRUE) |
| 88 | ->setLabel(t('Type')) |
| 89 | ->setSetting('target_type', 'crm_detail_type') |
| 90 | ->setDisplayOptions('form', [ |
| 91 | 'type' => 'options_select', |
| 92 | 'weight' => -10, |
| 93 | ]) |
| 94 | ->setDisplayConfigurable('form', TRUE) |
| 95 | ->setDisplayOptions('view', [ |
| 96 | 'label' => 'above', |
| 97 | 'type' => 'entity_reference_label', |
| 98 | 'weight' => 10, |
| 99 | ]) |
| 100 | ->setDisplayConfigurable('view', TRUE); |
| 101 | |
| 102 | $fields['status'] = BaseFieldDefinition::create('boolean') |
| 103 | ->setRevisionable(TRUE) |
| 104 | ->setLabel(t('Status')) |
| 105 | ->setDefaultValue(TRUE) |
| 106 | ->setSetting('on_label', 'Enabled') |
| 107 | ->setDisplayOptions('form', [ |
| 108 | 'type' => 'boolean_checkbox', |
| 109 | 'settings' => [ |
| 110 | 'display_label' => FALSE, |
| 111 | ], |
| 112 | 'weight' => 0, |
| 113 | ]) |
| 114 | ->setDisplayConfigurable('form', TRUE) |
| 115 | ->setDisplayOptions('view', [ |
| 116 | 'type' => 'boolean', |
| 117 | 'label' => 'above', |
| 118 | 'weight' => 0, |
| 119 | 'settings' => [ |
| 120 | 'format' => 'enabled-disabled', |
| 121 | ], |
| 122 | ]) |
| 123 | ->setDisplayConfigurable('view', TRUE); |
| 124 | |
| 125 | $fields['crm_contact'] = BaseFieldDefinition::create('entity_reference') |
| 126 | ->setLabel(t('CRM Contact')) |
| 127 | ->setDescription(t('The CRM Contact that this detail belongs to.')) |
| 128 | ->setSetting('target_type', 'crm_contact'); |
| 129 | |
| 130 | $fields['created'] = BaseFieldDefinition::create('created') |
| 131 | ->setLabel(t('Created on')) |
| 132 | ->setDescription(t('The time that the crm contact detail was created.')) |
| 133 | ->setDisplayOptions('view', [ |
| 134 | 'label' => 'above', |
| 135 | 'type' => 'timestamp', |
| 136 | 'weight' => 20, |
| 137 | ]) |
| 138 | ->setDisplayConfigurable('form', TRUE) |
| 139 | ->setDisplayOptions('form', [ |
| 140 | 'type' => 'datetime_timestamp', |
| 141 | 'weight' => 20, |
| 142 | ]) |
| 143 | ->setDisplayConfigurable('view', TRUE); |
| 144 | |
| 145 | $fields['changed'] = BaseFieldDefinition::create('changed') |
| 146 | ->setLabel(t('Changed')) |
| 147 | ->setDescription(t('The time that the crm contact detail was last edited.')); |
| 148 | |
| 149 | return $fields; |
| 150 | } |
| 151 | |
| 152 | } |