Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
85 / 85 |
|
100.00% |
10 / 10 |
CRAP | |
100.00% |
1 / 1 |
| UserContactMapping | |
100.00% |
85 / 85 |
|
100.00% |
10 / 10 |
12 | |
100.00% |
1 / 1 |
| getUser | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getUserId | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| setUserId | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| setUser | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| getContact | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getContactId | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| setContactId | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| setContact | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| baseFieldDefinitions | |
100.00% |
70 / 70 |
|
100.00% |
1 / 1 |
1 | |||
| label | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
3 | |||
| 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\ContentEntityBase; |
| 9 | use Drupal\Core\Entity\EntityTypeInterface; |
| 10 | use Drupal\Core\Entity\Routing\AdminHtmlRouteProvider; |
| 11 | use Drupal\Core\Field\BaseFieldDefinition; |
| 12 | use Drupal\Core\StringTranslation\TranslatableMarkup; |
| 13 | use Drupal\crm\Form\UserContactMappingForm; |
| 14 | use Drupal\crm\ListBuilder\UserContactMappingListBuilder; |
| 15 | use Drupal\crm\StorageSchema\UserContactMappingStorageSchema; |
| 16 | use Drupal\crm\UserContactMappingViewBuilder; |
| 17 | use Drupal\user\UserInterface; |
| 18 | use Drupal\views\EntityViewsData; |
| 19 | use Drupal\Core\Entity\ContentEntityDeleteForm; |
| 20 | |
| 21 | /** |
| 22 | * Defines the user contact mapping entity class. |
| 23 | */ |
| 24 | #[ContentEntityType( |
| 25 | id: 'crm_user_contact_mapping', |
| 26 | label: new TranslatableMarkup('User Contact Mapping'), |
| 27 | label_collection: new TranslatableMarkup('User Contact Mappings'), |
| 28 | handlers: [ |
| 29 | 'storage_schema' => UserContactMappingStorageSchema::class, |
| 30 | 'view_builder' => UserContactMappingViewBuilder::class, |
| 31 | 'list_builder' => UserContactMappingListBuilder::class, |
| 32 | 'views_data' => EntityViewsData::class, |
| 33 | 'form' => [ |
| 34 | 'default' => UserContactMappingForm::class, |
| 35 | 'delete' => ContentEntityDeleteForm::class, |
| 36 | ], |
| 37 | 'route_provider' => [ |
| 38 | 'html' => AdminHtmlRouteProvider::class, |
| 39 | ], |
| 40 | ], |
| 41 | base_table: 'crm_user_contact_mapping', |
| 42 | translatable: FALSE, |
| 43 | admin_permission: 'administer crm', |
| 44 | entity_keys: [ |
| 45 | 'id' => 'id', |
| 46 | 'uuid' => 'uuid', |
| 47 | ], |
| 48 | links: [ |
| 49 | 'add-form' => '/admin/config/crm/user/add', |
| 50 | 'canonical' => '/admin/config/crm/user/{crm_user_contact_mapping}', |
| 51 | 'edit-form' => '/admin/config/crm/user/{crm_user_contact_mapping}/edit', |
| 52 | 'delete-form' => '/admin/config/crm/user/{crm_user_contact_mapping}/delete', |
| 53 | 'collection' => '/admin/config/crm/user/list', |
| 54 | ], |
| 55 | )] |
| 56 | class UserContactMapping extends ContentEntityBase implements UserContactMappingInterface { |
| 57 | |
| 58 | /** |
| 59 | * {@inheritdoc} |
| 60 | */ |
| 61 | public function getUser() { |
| 62 | return $this->get('user')->entity; |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * {@inheritdoc} |
| 67 | */ |
| 68 | public function getUserId() { |
| 69 | return $this->get('user')->target_id; |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * {@inheritdoc} |
| 74 | */ |
| 75 | public function setUserId($uid) { |
| 76 | $this->set('user', $uid); |
| 77 | return $this; |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * {@inheritdoc} |
| 82 | */ |
| 83 | public function setUser(UserInterface $account) { |
| 84 | $this->set('user', $account->id()); |
| 85 | return $this; |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * {@inheritdoc} |
| 90 | */ |
| 91 | public function getContact() { |
| 92 | return $this->get('crm_contact')->entity; |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * {@inheritdoc} |
| 97 | */ |
| 98 | public function getContactId() { |
| 99 | return $this->get('crm_contact')->target_id; |
| 100 | } |
| 101 | |
| 102 | /** |
| 103 | * {@inheritdoc} |
| 104 | */ |
| 105 | public function setContactId($contact_id) { |
| 106 | $this->set('crm_contact', $contact_id); |
| 107 | return $this; |
| 108 | } |
| 109 | |
| 110 | /** |
| 111 | * {@inheritdoc} |
| 112 | */ |
| 113 | public function setContact(ContactInterface $person) { |
| 114 | $this->set('crm_contact', $person->id()); |
| 115 | return $this; |
| 116 | } |
| 117 | |
| 118 | /** |
| 119 | * {@inheritdoc} |
| 120 | */ |
| 121 | public static function baseFieldDefinitions(EntityTypeInterface $entity_type) { |
| 122 | |
| 123 | $fields = parent::baseFieldDefinitions($entity_type); |
| 124 | |
| 125 | $fields['user'] = BaseFieldDefinition::create('entity_reference') |
| 126 | ->setRequired(TRUE) |
| 127 | ->setCardinality(1) |
| 128 | // @todo Replace with what ever would work with entity reference |
| 129 | // from core. https://www.drupal.org/project/drupal/issues/2478663 |
| 130 | ->addConstraint('UniqueField') |
| 131 | ->setLabel(t('User')) |
| 132 | ->setDescription(t('The user ID of the relation.')) |
| 133 | ->setSetting('target_type', 'user') |
| 134 | ->setDisplayOptions('form', [ |
| 135 | 'type' => 'entity_reference_autocomplete', |
| 136 | 'settings' => [ |
| 137 | 'match_operator' => 'CONTAINS', |
| 138 | 'size' => 60, |
| 139 | 'placeholder' => '', |
| 140 | ], |
| 141 | 'weight' => 10, |
| 142 | ]) |
| 143 | ->setDisplayConfigurable('form', TRUE) |
| 144 | ->setDisplayOptions('view', [ |
| 145 | 'label' => 'above', |
| 146 | 'type' => 'entity_reference_label', |
| 147 | 'weight' => 10, |
| 148 | ]) |
| 149 | ->setDisplayConfigurable('view', TRUE); |
| 150 | |
| 151 | $fields['crm_contact'] = BaseFieldDefinition::create('entity_reference') |
| 152 | ->setRequired(TRUE) |
| 153 | ->setCardinality(1) |
| 154 | // @todo Replace with what ever would work with entity reference |
| 155 | // from core. https://www.drupal.org/project/drupal/issues/2478663 |
| 156 | ->addConstraint('UniqueField') |
| 157 | ->setLabel(t('Person')) |
| 158 | ->setDescription(t('The person ID of the relation.')) |
| 159 | ->setSetting('target_type', 'crm_contact') |
| 160 | ->setSetting('handler_settings', ['target_bundles' => ['person' => 'person']]) |
| 161 | ->setDisplayOptions('form', [ |
| 162 | 'type' => 'entity_reference_autocomplete', |
| 163 | 'settings' => [ |
| 164 | 'match_operator' => 'CONTAINS', |
| 165 | 'size' => 60, |
| 166 | 'placeholder' => '', |
| 167 | ], |
| 168 | 'weight' => 15, |
| 169 | ]) |
| 170 | ->setDisplayConfigurable('form', TRUE) |
| 171 | ->setDisplayOptions('view', [ |
| 172 | 'label' => 'above', |
| 173 | 'type' => 'entity_reference_label', |
| 174 | 'weight' => 15, |
| 175 | ]) |
| 176 | ->setDisplayConfigurable('view', TRUE); |
| 177 | |
| 178 | $fields['created'] = BaseFieldDefinition::create('created') |
| 179 | ->setLabel(t('Created on')) |
| 180 | ->setDescription(t('The time that the contact was created.')) |
| 181 | ->setDisplayOptions('view', [ |
| 182 | 'label' => 'above', |
| 183 | 'type' => 'timestamp', |
| 184 | 'weight' => 20, |
| 185 | ]) |
| 186 | ->setDisplayConfigurable('form', TRUE) |
| 187 | ->setDisplayOptions('form', [ |
| 188 | 'type' => 'datetime_timestamp', |
| 189 | 'weight' => 20, |
| 190 | ]) |
| 191 | ->setDisplayConfigurable('view', TRUE); |
| 192 | |
| 193 | $fields['changed'] = BaseFieldDefinition::create('changed') |
| 194 | ->setLabel(t('Changed')) |
| 195 | ->setDescription(t('The time that the contact was last edited.')); |
| 196 | |
| 197 | return $fields; |
| 198 | } |
| 199 | |
| 200 | /** |
| 201 | * {@inheritdoc} |
| 202 | */ |
| 203 | public function label() { |
| 204 | $user = $this->getUser(); |
| 205 | $contact = $this->getContact(); |
| 206 | |
| 207 | return ($user ? $user->getAccountName() : 'Unknown User') . ' - ' . ($contact ? $contact->label() : 'Unknown Contact'); |
| 208 | } |
| 209 | |
| 210 | } |