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