Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 98 |
|
0.00% |
0 / 4 |
CRAP | |
0.00% |
0 / 1 |
ContactForm | |
0.00% |
0 / 98 |
|
0.00% |
0 / 4 |
552 | |
0.00% |
0 / 1 |
__construct | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
create | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
2 | |||
form | |
0.00% |
0 / 59 |
|
0.00% |
0 / 1 |
272 | |||
save | |
0.00% |
0 / 29 |
|
0.00% |
0 / 1 |
30 |
1 | <?php |
2 | |
3 | namespace Drupal\crm\Form; |
4 | |
5 | use Drupal\Component\Datetime\TimeInterface; |
6 | use Drupal\Core\Datetime\DateFormatterInterface; |
7 | use Drupal\Core\Entity\ContentEntityForm; |
8 | use Drupal\Core\Entity\EntityRepositoryInterface; |
9 | use Drupal\Core\Entity\EntityTypeBundleInfoInterface; |
10 | use Drupal\Core\Form\FormStateInterface; |
11 | use Drupal\Core\Session\AccountInterface; |
12 | use Symfony\Component\DependencyInjection\ContainerInterface; |
13 | |
14 | /** |
15 | * Form controller for the contact entity edit forms. |
16 | */ |
17 | class ContactForm extends ContentEntityForm { |
18 | |
19 | /** |
20 | * The date formatter service. |
21 | * |
22 | * @var \Drupal\Core\Datetime\DateFormatterInterface |
23 | */ |
24 | protected $dateFormatter; |
25 | |
26 | /** |
27 | * The current user. |
28 | * |
29 | * @var \Drupal\Core\Session\AccountInterface |
30 | */ |
31 | protected $currentUser; |
32 | |
33 | /** |
34 | * Constructs a ContactForm object. |
35 | * |
36 | * @param \Drupal\Core\Entity\EntityRepositoryInterface $entity_repository |
37 | * The entity repository. |
38 | * @param \Drupal\Component\Datetime\TimeInterface $time |
39 | * The time service. |
40 | * @param \Drupal\Core\Datetime\DateFormatterInterface $date_formatter |
41 | * The date formatter service. |
42 | * @param \Drupal\Core\Session\AccountInterface $current_user |
43 | * The current user. |
44 | * @param \Drupal\Core\Entity\EntityTypeBundleInfoInterface|null $entity_type_bundle_info |
45 | * The entity type bundle service. |
46 | */ |
47 | public function __construct( |
48 | EntityRepositoryInterface $entity_repository, |
49 | TimeInterface $time, |
50 | DateFormatterInterface $date_formatter, |
51 | AccountInterface $current_user, |
52 | ?EntityTypeBundleInfoInterface $entity_type_bundle_info = NULL, |
53 | ) { |
54 | |
55 | parent::__construct($entity_repository, $entity_type_bundle_info, $time); |
56 | $this->dateFormatter = $date_formatter; |
57 | $this->currentUser = $current_user; |
58 | } |
59 | |
60 | /** |
61 | * {@inheritdoc} |
62 | */ |
63 | final public static function create(ContainerInterface $container) { |
64 | return new self( |
65 | $container->get('entity.repository'), |
66 | $container->get('datetime.time'), |
67 | $container->get('date.formatter'), |
68 | $container->get('current_user'), |
69 | $container->get('entity_type.bundle.info'), |
70 | ); |
71 | } |
72 | |
73 | /** |
74 | * {@inheritdoc} |
75 | */ |
76 | public function form(array $form, FormStateInterface $form_state) { |
77 | /** @var \Drupal\crm\CrmContactInterface $contact */ |
78 | $contact = $this->entity; |
79 | $contact_type = $contact->get('bundle')->entity; |
80 | $date = $contact_type->get('date'); |
81 | $form = parent::form($form, $form_state); |
82 | |
83 | $form['advanced']['#attributes']['class'][] = 'entity-meta'; |
84 | |
85 | $form['meta'] = [ |
86 | '#type' => 'details', |
87 | '#group' => 'advanced', |
88 | '#weight' => -10, |
89 | '#title' => $this->t('Status'), |
90 | '#attributes' => ['class' => ['entity-meta__header']], |
91 | '#tree' => TRUE, |
92 | '#access' => $this->currentUser->hasPermission('administer crm'), |
93 | ]; |
94 | $form['meta']['published'] = [ |
95 | '#type' => 'item', |
96 | '#markup' => $contact->isPublished() ? $this->t('Active') : $this->t('Inactive'), |
97 | '#access' => !$contact->isNew(), |
98 | '#wrapper_attributes' => ['class' => ['entity-meta__title']], |
99 | ]; |
100 | $form['meta']['changed'] = [ |
101 | '#type' => 'item', |
102 | '#title' => $this->t('Last saved'), |
103 | '#markup' => !$contact->isNew() ? $this->dateFormatter->format($contact->getChangedTime(), 'short') : $this->t('Not saved yet'), |
104 | '#wrapper_attributes' => ['class' => ['entity-meta__last-saved']], |
105 | ]; |
106 | |
107 | $form['meta']['status'] = &$form['status']; |
108 | unset($form['status']); |
109 | |
110 | if (isset($form['created'])) { |
111 | $form['created']['#weight'] = 200; |
112 | $form['meta']['created'] = &$form['created']; |
113 | unset($form['created']); |
114 | } |
115 | |
116 | $form['age'] = [ |
117 | '#type' => 'details', |
118 | '#title' => $this->t('Age'), |
119 | '#group' => 'advanced', |
120 | ]; |
121 | |
122 | if ($date && !empty($date['start_date']['label']) && isset($form['start_date']['widget'])) { |
123 | $form['start_date']['widget'][0]['#title'] = $date['start_date']['label']; |
124 | $form['start_date']['widget']['#title'] = $date['start_date']['label']; |
125 | $form['start_date']['widget'][0]['value']['#title'] = $date['start_date']['label']; |
126 | } |
127 | |
128 | if ($date && !empty($date['start_date']['description']) && isset($form['start_date']['widget'])) { |
129 | $form['start_date']['widget'][0]['#description'] = $date['start_date']['description']; |
130 | $form['start_date']['widget']['#description'] = $date['start_date']['description']; |
131 | $form['start_date']['widget'][0]['value']['#description'] = $date['start_date']['description']; |
132 | } |
133 | |
134 | if ($date && !empty($date['end_date']['label']) && isset($form['end_date']['widget'])) { |
135 | $form['end_date']['widget'][0]['#title'] = $date['end_date']['label']; |
136 | $form['end_date']['widget']['#title'] = $date['end_date']['label']; |
137 | $form['end_date']['widget'][0]['value']['#title'] = $date['end_date']['label']; |
138 | } |
139 | |
140 | if ($date && !empty($date['end_date']['description']) && isset($form['end_date']['widget'])) { |
141 | $form['end_date']['widget'][0]['#description'] = $date['end_date']['description']; |
142 | $form['end_date']['widget']['#description'] = $date['end_date']['description']; |
143 | $form['end_date']['widget'][0]['value']['#description'] = $date['end_date']['description']; |
144 | } |
145 | |
146 | $form['age']['start_date'] = &$form['start_date']; |
147 | unset($form['start_date']); |
148 | |
149 | $form['age']['end_date'] = &$form['end_date']; |
150 | unset($form['end_date']); |
151 | |
152 | // Add a custom submit handler to the entity form. |
153 | $form['revision']['#default_value'] = TRUE; |
154 | |
155 | return $form; |
156 | } |
157 | |
158 | /** |
159 | * {@inheritdoc} |
160 | */ |
161 | public function save(array $form, FormStateInterface $form_state) { |
162 | |
163 | $contact = $this->getEntity(); |
164 | // Set new Revision. |
165 | $contact->setNewRevision(TRUE); |
166 | |
167 | $result = parent::save($form, $form_state); |
168 | |
169 | $message_arguments = ['%label' => $contact->toLink()->toString()]; |
170 | $logger_arguments = [ |
171 | '%label' => $contact->label(), |
172 | 'link' => $contact->toLink($this->t('View'))->toString(), |
173 | ]; |
174 | |
175 | switch ($result) { |
176 | case SAVED_NEW: |
177 | $this->messenger()->addStatus($this->t('New contact %label has been created.', $message_arguments)); |
178 | $this->logger('crm_contact')->notice('Created new contact %label', $logger_arguments); |
179 | break; |
180 | |
181 | case SAVED_UPDATED: |
182 | $this->messenger()->addStatus($this->t('The contact %label has been updated.', $message_arguments)); |
183 | $this->logger('crm_contact')->notice('Updated contact %label.', $logger_arguments); |
184 | break; |
185 | } |
186 | |
187 | $form_state->setRedirect('entity.crm_contact.canonical', [ |
188 | 'crm_contact' => $contact->id(), |
189 | ]); |
190 | |
191 | if ($contact->id()) { |
192 | $form_state->setValue('id', $contact->id()); |
193 | $form_state->set('id', $contact->id()); |
194 | if ($contact->access('view')) { |
195 | $form_state->setRedirect( |
196 | 'entity.crm_contact.canonical', |
197 | ['crm_contact' => $contact->id()] |
198 | ); |
199 | } |
200 | else { |
201 | $form_state->setRedirect('<front>'); |
202 | } |
203 | |
204 | } |
205 | |
206 | return $result; |
207 | } |
208 | |
209 | } |