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