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