Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 26 |
|
0.00% |
0 / 2 |
CRAP | |
0.00% |
0 / 1 |
UserContactForm | |
0.00% |
0 / 26 |
|
0.00% |
0 / 2 |
72 | |
0.00% |
0 / 1 |
form | |
0.00% |
0 / 15 |
|
0.00% |
0 / 1 |
42 | |||
save | |
0.00% |
0 / 11 |
|
0.00% |
0 / 1 |
6 |
1 | <?php |
2 | |
3 | namespace Drupal\crm\Form; |
4 | |
5 | use Drupal\Core\Entity\ContentEntityForm; |
6 | use Drupal\Core\Form\FormStateInterface; |
7 | |
8 | /** |
9 | * Form controller for the relation entity edit forms. |
10 | */ |
11 | class UserContactForm extends ContentEntityForm { |
12 | |
13 | /** |
14 | * {@inheritdoc} |
15 | */ |
16 | public function form(array $form, FormStateInterface $form_state) { |
17 | $relation = $this->getEntity(); |
18 | |
19 | $query = $this->getRequest()->query; |
20 | $crm_contact_id = $query->get('crm_contact'); |
21 | $user_id = $query->get('user'); |
22 | if ($crm_contact_id || $user_id) { |
23 | $relation->setContactId($crm_contact_id); |
24 | $relation->setUserId($user_id); |
25 | $this->setEntity($relation); |
26 | } |
27 | |
28 | $form = parent::form($form, $form_state); |
29 | $is_new = $relation->isNew(); |
30 | |
31 | if ($crm_contact_id) { |
32 | $form['crm_contact']['widget']['#disabled'] = TRUE; |
33 | } |
34 | |
35 | if ($user_id || !$is_new) { |
36 | $form['user']['widget']['#disabled'] = TRUE; |
37 | } |
38 | |
39 | return $form; |
40 | } |
41 | |
42 | /** |
43 | * {@inheritdoc} |
44 | */ |
45 | public function save(array $form, FormStateInterface $form_state) { |
46 | |
47 | $entity = $this->getEntity(); |
48 | $result = $entity->save(); |
49 | $link = $entity->toLink($this->t('View'))->toString(); |
50 | |
51 | $logger_arguments = ['link' => $link]; |
52 | |
53 | if ($result == SAVED_NEW) { |
54 | $this->messenger()->addMessage($this->t('New relation has been created.')); |
55 | $this->logger('crm')->notice('Created new relation', $logger_arguments); |
56 | } |
57 | else { |
58 | $this->messenger()->addMessage($this->t('The relation has been updated.')); |
59 | $this->logger('crm')->notice('Relation updated', $logger_arguments); |
60 | } |
61 | |
62 | $form_state->setRedirect('entity.crm_user_contact.collection'); |
63 | |
64 | return $result; |
65 | } |
66 | |
67 | } |