Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
115 / 115 |
|
100.00% |
2 / 2 |
CRAP | |
100.00% |
1 / 1 |
| CrmCase | |
100.00% |
115 / 115 |
|
100.00% |
2 / 2 |
3 | |
100.00% |
1 / 1 |
| preSave | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
| baseFieldDefinitions | |
100.00% |
112 / 112 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace Drupal\crm_case\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\EntityStorageInterface; |
| 11 | use Drupal\Core\Entity\EntityTypeInterface; |
| 12 | use Drupal\Core\Entity\RevisionableContentEntityBase; |
| 13 | use Drupal\Core\Entity\Routing\AdminHtmlRouteProvider; |
| 14 | use Drupal\Core\Field\BaseFieldDefinition; |
| 15 | use Drupal\Core\StringTranslation\TranslatableMarkup; |
| 16 | use Drupal\crm_case\CrmCaseAccessControlHandler; |
| 17 | use Drupal\crm_case\CrmCaseInterface; |
| 18 | use Drupal\crm_case\CrmCaseListBuilder; |
| 19 | use Drupal\crm_case\Form\CrmCaseForm; |
| 20 | use Drupal\user\EntityOwnerTrait; |
| 21 | use Drupal\views\EntityViewsData; |
| 22 | |
| 23 | /** |
| 24 | * CRM case. |
| 25 | * |
| 26 | * A case is a collection of encounters. |
| 27 | */ |
| 28 | #[ContentEntityType( |
| 29 | id: 'crm_case', |
| 30 | label: new TranslatableMarkup('CRM Case'), |
| 31 | label_collection: new TranslatableMarkup('CRM Cases'), |
| 32 | label_singular: new TranslatableMarkup('crm case'), |
| 33 | label_plural: new TranslatableMarkup('crm cases'), |
| 34 | label_count: [ |
| 35 | 'singular' => '@count crm case', |
| 36 | 'plural' => '@count crm cases', |
| 37 | ], |
| 38 | bundle_label: new TranslatableMarkup('CRM Case type'), |
| 39 | handlers: [ |
| 40 | 'list_builder' => CrmCaseListBuilder::class, |
| 41 | 'views_data' => EntityViewsData::class, |
| 42 | 'form' => [ |
| 43 | 'default' => CrmCaseForm::class, |
| 44 | 'delete' => ContentEntityDeleteForm::class, |
| 45 | ], |
| 46 | 'access' => CrmCaseAccessControlHandler::class, |
| 47 | 'route_provider' => [ |
| 48 | 'html' => AdminHtmlRouteProvider::class, |
| 49 | ], |
| 50 | ], |
| 51 | base_table: 'crm_case', |
| 52 | revision_table: 'crm_case_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 | 'label' => 'label', |
| 60 | 'uuid' => 'uuid', |
| 61 | 'owner' => 'uid', |
| 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_case_type', |
| 69 | field_ui_base_route: 'entity.crm_case_type.edit_form', |
| 70 | translatable: FALSE, |
| 71 | links: [ |
| 72 | 'collection' => '/admin/content/crm/case', |
| 73 | 'add-form' => '/crm/case/add/{crm_case_type}', |
| 74 | 'add-page' => '/crm/case/add', |
| 75 | 'canonical' => '/crm/case/{crm_case}', |
| 76 | 'edit-form' => '/crm/case/{crm_case}/edit', |
| 77 | 'delete-form' => '/crm/case/{crm_case}/delete', |
| 78 | ] |
| 79 | )] |
| 80 | class CrmCase extends RevisionableContentEntityBase implements CrmCaseInterface { |
| 81 | |
| 82 | use EntityChangedTrait; |
| 83 | use EntityOwnerTrait; |
| 84 | |
| 85 | /** |
| 86 | * {@inheritdoc} |
| 87 | */ |
| 88 | public function preSave(EntityStorageInterface $storage) { |
| 89 | parent::preSave($storage); |
| 90 | if (!$this->getOwnerId()) { |
| 91 | // If no owner has been set explicitly, make the anonymous user the owner. |
| 92 | $this->setOwnerId(0); |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * {@inheritdoc} |
| 98 | */ |
| 99 | public static function baseFieldDefinitions(EntityTypeInterface $entity_type) { |
| 100 | |
| 101 | $fields = parent::baseFieldDefinitions($entity_type); |
| 102 | $fields['bundle']->setLabel(t('Type')) |
| 103 | ->setDisplayOptions('view', [ |
| 104 | 'type' => 'text_default', |
| 105 | 'label' => 'inline', |
| 106 | 'weight' => 10, |
| 107 | ]) |
| 108 | ->setDisplayConfigurable('view', TRUE); |
| 109 | |
| 110 | $fields['label'] = BaseFieldDefinition::create('string') |
| 111 | ->setRevisionable(TRUE) |
| 112 | ->setLabel(t('Label')) |
| 113 | ->setRequired(TRUE) |
| 114 | ->setSetting('max_length', 255) |
| 115 | ->setDisplayOptions('form', [ |
| 116 | 'type' => 'string_textfield', |
| 117 | 'weight' => -5, |
| 118 | ]) |
| 119 | ->setDisplayConfigurable('form', TRUE) |
| 120 | ->setDisplayOptions('view', [ |
| 121 | 'label' => 'hidden', |
| 122 | 'type' => 'string', |
| 123 | 'weight' => -5, |
| 124 | ]) |
| 125 | ->setDisplayConfigurable('view', TRUE); |
| 126 | |
| 127 | $fields['status'] = BaseFieldDefinition::create('boolean') |
| 128 | ->setRevisionable(TRUE) |
| 129 | ->setLabel(t('Status')) |
| 130 | ->setDefaultValue(TRUE) |
| 131 | ->setSetting('on_label', 'Enabled') |
| 132 | ->setDisplayOptions('form', [ |
| 133 | 'type' => 'boolean_checkbox', |
| 134 | 'settings' => [ |
| 135 | 'display_label' => FALSE, |
| 136 | ], |
| 137 | 'weight' => 0, |
| 138 | ]) |
| 139 | ->setDisplayConfigurable('form', TRUE) |
| 140 | ->setDisplayOptions('view', [ |
| 141 | 'type' => 'boolean', |
| 142 | 'label' => 'inline', |
| 143 | 'weight' => 0, |
| 144 | 'settings' => [ |
| 145 | 'format' => 'enabled-disabled', |
| 146 | ], |
| 147 | ]) |
| 148 | ->setDisplayConfigurable('view', TRUE); |
| 149 | |
| 150 | $fields['uid'] = BaseFieldDefinition::create('entity_reference') |
| 151 | ->setRevisionable(TRUE) |
| 152 | ->setLabel(t('Owner')) |
| 153 | ->setSetting('target_type', 'user') |
| 154 | ->setDefaultValueCallback(static::class . '::getDefaultEntityOwner') |
| 155 | ->setDisplayOptions('form', [ |
| 156 | 'type' => 'entity_reference_autocomplete', |
| 157 | 'settings' => [ |
| 158 | 'match_operator' => 'CONTAINS', |
| 159 | 'size' => 60, |
| 160 | 'placeholder' => '', |
| 161 | ], |
| 162 | 'weight' => 15, |
| 163 | ]) |
| 164 | ->setDisplayConfigurable('form', TRUE) |
| 165 | ->setDisplayOptions('view', [ |
| 166 | 'label' => 'inline', |
| 167 | 'type' => 'author', |
| 168 | 'weight' => 15, |
| 169 | ]) |
| 170 | ->setDisplayConfigurable('view', TRUE); |
| 171 | |
| 172 | $fields['contact_id'] = BaseFieldDefinition::create('entity_reference') |
| 173 | ->setRevisionable(TRUE) |
| 174 | ->setLabel(t('Contact')) |
| 175 | ->setSetting('target_type', 'crm_contact') |
| 176 | ->setDefaultValueCallback(static::class . '::getDefaultEntityOwner') |
| 177 | ->setDisplayOptions('form', [ |
| 178 | 'type' => 'entity_reference_autocomplete', |
| 179 | 'settings' => [ |
| 180 | 'match_operator' => 'CONTAINS', |
| 181 | 'size' => 60, |
| 182 | 'placeholder' => '', |
| 183 | ], |
| 184 | 'weight' => 15, |
| 185 | ]) |
| 186 | ->setDisplayConfigurable('form', TRUE) |
| 187 | ->setDisplayOptions('view', [ |
| 188 | 'label' => 'inline', |
| 189 | 'type' => 'author', |
| 190 | 'weight' => 15, |
| 191 | ]) |
| 192 | ->setDisplayConfigurable('view', TRUE); |
| 193 | |
| 194 | $fields['created'] = BaseFieldDefinition::create('created') |
| 195 | ->setLabel(t('Created')) |
| 196 | ->setDescription(t('The time that the crm case was created.')) |
| 197 | ->setDisplayOptions('view', [ |
| 198 | 'label' => 'inline', |
| 199 | 'type' => 'timestamp', |
| 200 | 'weight' => 20, |
| 201 | ]) |
| 202 | ->setDisplayConfigurable('form', TRUE) |
| 203 | ->setDisplayOptions('form', [ |
| 204 | 'type' => 'datetime_timestamp', |
| 205 | 'weight' => 20, |
| 206 | ]) |
| 207 | ->setDisplayConfigurable('view', TRUE); |
| 208 | |
| 209 | $fields['changed'] = BaseFieldDefinition::create('changed') |
| 210 | ->setLabel(t('Changed')) |
| 211 | ->setDescription(t('The time that the crm case was last edited.')) |
| 212 | ->setDisplayOptions('view', [ |
| 213 | 'label' => 'inline', |
| 214 | 'type' => 'timestamp', |
| 215 | 'weight' => 20, |
| 216 | ]) |
| 217 | ->setDisplayConfigurable('view', TRUE); |
| 218 | |
| 219 | return $fields; |
| 220 | } |
| 221 | |
| 222 | } |