Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 namespace Drupal\contact;
|
Chris@0
|
4
|
Chris@0
|
5 use Drupal\Component\Datetime\TimeInterface;
|
Chris@0
|
6 use Drupal\Core\Datetime\DateFormatterInterface;
|
Chris@0
|
7 use Drupal\Core\Entity\ContentEntityForm;
|
Chris@17
|
8 use Drupal\Core\Entity\EntityRepositoryInterface;
|
Chris@0
|
9 use Drupal\Core\Entity\EntityTypeBundleInfoInterface;
|
Chris@0
|
10 use Drupal\Core\Flood\FloodInterface;
|
Chris@0
|
11 use Drupal\Core\Form\FormStateInterface;
|
Chris@0
|
12 use Drupal\Core\Language\LanguageManagerInterface;
|
Chris@0
|
13 use Symfony\Component\DependencyInjection\ContainerInterface;
|
Chris@0
|
14
|
Chris@0
|
15 /**
|
Chris@0
|
16 * Form controller for contact message forms.
|
Chris@14
|
17 *
|
Chris@14
|
18 * @internal
|
Chris@0
|
19 */
|
Chris@0
|
20 class MessageForm extends ContentEntityForm {
|
Chris@0
|
21
|
Chris@0
|
22 /**
|
Chris@0
|
23 * The message being used by this form.
|
Chris@0
|
24 *
|
Chris@0
|
25 * @var \Drupal\contact\MessageInterface
|
Chris@0
|
26 */
|
Chris@0
|
27 protected $entity;
|
Chris@0
|
28
|
Chris@0
|
29 /**
|
Chris@0
|
30 * The flood control mechanism.
|
Chris@0
|
31 *
|
Chris@0
|
32 * @var \Drupal\Core\Flood\FloodInterface
|
Chris@0
|
33 */
|
Chris@0
|
34 protected $flood;
|
Chris@0
|
35
|
Chris@0
|
36 /**
|
Chris@0
|
37 * The language manager service.
|
Chris@0
|
38 *
|
Chris@0
|
39 * @var \Drupal\Core\Language\LanguageManagerInterface
|
Chris@0
|
40 */
|
Chris@0
|
41 protected $languageManager;
|
Chris@0
|
42
|
Chris@0
|
43 /**
|
Chris@0
|
44 * The contact mail handler service.
|
Chris@0
|
45 *
|
Chris@0
|
46 * @var \Drupal\contact\MailHandlerInterface
|
Chris@0
|
47 */
|
Chris@0
|
48 protected $mailHandler;
|
Chris@0
|
49
|
Chris@0
|
50 /**
|
Chris@0
|
51 * The date formatter service.
|
Chris@0
|
52 *
|
Chris@0
|
53 * @var \Drupal\Core\Datetime\DateFormatterInterface
|
Chris@0
|
54 */
|
Chris@0
|
55 protected $dateFormatter;
|
Chris@0
|
56
|
Chris@0
|
57 /**
|
Chris@0
|
58 * Constructs a MessageForm object.
|
Chris@0
|
59 *
|
Chris@17
|
60 * @param \Drupal\Core\Entity\EntityRepositoryInterface $entity_repository
|
Chris@17
|
61 * The entity repository.
|
Chris@0
|
62 * @param \Drupal\Core\Flood\FloodInterface $flood
|
Chris@0
|
63 * The flood control mechanism.
|
Chris@0
|
64 * @param \Drupal\Core\Language\LanguageManagerInterface $language_manager
|
Chris@0
|
65 * The language manager service.
|
Chris@0
|
66 * @param \Drupal\contact\MailHandlerInterface $mail_handler
|
Chris@0
|
67 * The contact mail handler service.
|
Chris@0
|
68 * @param \Drupal\Core\Datetime\DateFormatterInterface $date_formatter
|
Chris@0
|
69 * The date service.
|
Chris@0
|
70 * @param \Drupal\Core\Entity\EntityTypeBundleInfoInterface $entity_type_bundle_info
|
Chris@0
|
71 * The entity type bundle service.
|
Chris@0
|
72 * @param \Drupal\Component\Datetime\TimeInterface $time
|
Chris@0
|
73 * The time service.
|
Chris@0
|
74 */
|
Chris@17
|
75 public function __construct(EntityRepositoryInterface $entity_repository, FloodInterface $flood, LanguageManagerInterface $language_manager, MailHandlerInterface $mail_handler, DateFormatterInterface $date_formatter, EntityTypeBundleInfoInterface $entity_type_bundle_info = NULL, TimeInterface $time = NULL) {
|
Chris@17
|
76 parent::__construct($entity_repository, $entity_type_bundle_info, $time);
|
Chris@0
|
77 $this->flood = $flood;
|
Chris@0
|
78 $this->languageManager = $language_manager;
|
Chris@0
|
79 $this->mailHandler = $mail_handler;
|
Chris@0
|
80 $this->dateFormatter = $date_formatter;
|
Chris@0
|
81 }
|
Chris@0
|
82
|
Chris@0
|
83 /**
|
Chris@0
|
84 * {@inheritdoc}
|
Chris@0
|
85 */
|
Chris@0
|
86 public static function create(ContainerInterface $container) {
|
Chris@0
|
87 return new static(
|
Chris@17
|
88 $container->get('entity.repository'),
|
Chris@0
|
89 $container->get('flood'),
|
Chris@0
|
90 $container->get('language_manager'),
|
Chris@0
|
91 $container->get('contact.mail_handler'),
|
Chris@0
|
92 $container->get('date.formatter'),
|
Chris@0
|
93 $container->get('entity_type.bundle.info'),
|
Chris@0
|
94 $container->get('datetime.time')
|
Chris@0
|
95 );
|
Chris@0
|
96 }
|
Chris@0
|
97
|
Chris@0
|
98 /**
|
Chris@0
|
99 * {@inheritdoc}
|
Chris@0
|
100 */
|
Chris@0
|
101 public function form(array $form, FormStateInterface $form_state) {
|
Chris@0
|
102 $user = $this->currentUser();
|
Chris@0
|
103 $message = $this->entity;
|
Chris@0
|
104 $form = parent::form($form, $form_state, $message);
|
Chris@0
|
105 $form['#attributes']['class'][] = 'contact-form';
|
Chris@0
|
106
|
Chris@0
|
107 if (!empty($message->preview)) {
|
Chris@0
|
108 $form['preview'] = [
|
Chris@0
|
109 '#theme_wrappers' => ['container__preview'],
|
Chris@0
|
110 '#attributes' => ['class' => ['preview']],
|
Chris@0
|
111 ];
|
Chris@17
|
112 $form['preview']['message'] = $this->entityTypeManager->getViewBuilder('contact_message')->view($message, 'full');
|
Chris@0
|
113 }
|
Chris@0
|
114
|
Chris@0
|
115 $form['name'] = [
|
Chris@0
|
116 '#type' => 'textfield',
|
Chris@0
|
117 '#title' => $this->t('Your name'),
|
Chris@0
|
118 '#maxlength' => 255,
|
Chris@0
|
119 '#required' => TRUE,
|
Chris@0
|
120 ];
|
Chris@0
|
121 $form['mail'] = [
|
Chris@0
|
122 '#type' => 'email',
|
Chris@0
|
123 '#title' => $this->t('Your email address'),
|
Chris@0
|
124 '#required' => TRUE,
|
Chris@0
|
125 ];
|
Chris@0
|
126 if ($user->isAnonymous()) {
|
Chris@0
|
127 $form['#attached']['library'][] = 'core/drupal.form';
|
Chris@0
|
128 $form['#attributes']['data-user-info-from-browser'] = TRUE;
|
Chris@0
|
129 }
|
Chris@0
|
130 // Do not allow authenticated users to alter the name or email values to
|
Chris@0
|
131 // prevent the impersonation of other users.
|
Chris@0
|
132 else {
|
Chris@0
|
133 $form['name']['#type'] = 'item';
|
Chris@0
|
134 $form['name']['#value'] = $user->getDisplayName();
|
Chris@0
|
135 $form['name']['#required'] = FALSE;
|
Chris@0
|
136 $form['name']['#plain_text'] = $user->getDisplayName();
|
Chris@0
|
137
|
Chris@0
|
138 $form['mail']['#type'] = 'item';
|
Chris@0
|
139 $form['mail']['#value'] = $user->getEmail();
|
Chris@0
|
140 $form['mail']['#required'] = FALSE;
|
Chris@0
|
141 $form['mail']['#plain_text'] = $user->getEmail();
|
Chris@0
|
142 }
|
Chris@0
|
143
|
Chris@0
|
144 // The user contact form has a preset recipient.
|
Chris@0
|
145 if ($message->isPersonal()) {
|
Chris@0
|
146 $form['recipient'] = [
|
Chris@0
|
147 '#type' => 'item',
|
Chris@0
|
148 '#title' => $this->t('To'),
|
Chris@0
|
149 '#value' => $message->getPersonalRecipient()->id(),
|
Chris@0
|
150 'name' => [
|
Chris@0
|
151 '#theme' => 'username',
|
Chris@0
|
152 '#account' => $message->getPersonalRecipient(),
|
Chris@0
|
153 ],
|
Chris@0
|
154 ];
|
Chris@0
|
155 }
|
Chris@0
|
156
|
Chris@0
|
157 $form['copy'] = [
|
Chris@0
|
158 '#type' => 'checkbox',
|
Chris@0
|
159 '#title' => $this->t('Send yourself a copy'),
|
Chris@0
|
160 // Do not allow anonymous users to send themselves a copy, because it can
|
Chris@0
|
161 // be abused to spam people.
|
Chris@0
|
162 '#access' => $user->isAuthenticated(),
|
Chris@0
|
163 ];
|
Chris@0
|
164 return $form;
|
Chris@0
|
165 }
|
Chris@0
|
166
|
Chris@0
|
167 /**
|
Chris@0
|
168 * {@inheritdoc}
|
Chris@0
|
169 */
|
Chris@0
|
170 public function actions(array $form, FormStateInterface $form_state) {
|
Chris@0
|
171 $elements = parent::actions($form, $form_state);
|
Chris@0
|
172 $elements['submit']['#value'] = $this->t('Send message');
|
Chris@0
|
173 $elements['preview'] = [
|
Chris@0
|
174 '#type' => 'submit',
|
Chris@0
|
175 '#value' => $this->t('Preview'),
|
Chris@0
|
176 '#submit' => ['::submitForm', '::preview'],
|
Chris@0
|
177 ];
|
Chris@0
|
178 return $elements;
|
Chris@0
|
179 }
|
Chris@0
|
180
|
Chris@0
|
181 /**
|
Chris@0
|
182 * Form submission handler for the 'preview' action.
|
Chris@0
|
183 */
|
Chris@0
|
184 public function preview(array $form, FormStateInterface $form_state) {
|
Chris@0
|
185 $message = $this->entity;
|
Chris@0
|
186 $message->preview = TRUE;
|
Chris@0
|
187 $form_state->setRebuild();
|
Chris@0
|
188 }
|
Chris@0
|
189
|
Chris@0
|
190 /**
|
Chris@0
|
191 * {@inheritdoc}
|
Chris@0
|
192 */
|
Chris@0
|
193 public function validateForm(array &$form, FormStateInterface $form_state) {
|
Chris@0
|
194 $message = parent::validateForm($form, $form_state);
|
Chris@0
|
195
|
Chris@0
|
196 // Check if flood control has been activated for sending emails.
|
Chris@0
|
197 if (!$this->currentUser()->hasPermission('administer contact forms') && (!$message->isPersonal() || !$this->currentUser()->hasPermission('administer users'))) {
|
Chris@0
|
198 $limit = $this->config('contact.settings')->get('flood.limit');
|
Chris@0
|
199 $interval = $this->config('contact.settings')->get('flood.interval');
|
Chris@0
|
200
|
Chris@0
|
201 if (!$this->flood->isAllowed('contact', $limit, $interval)) {
|
Chris@0
|
202 $form_state->setErrorByName('', $this->t('You cannot send more than %limit messages in @interval. Try again later.', [
|
Chris@0
|
203 '%limit' => $limit,
|
Chris@0
|
204 '@interval' => $this->dateFormatter->formatInterval($interval),
|
Chris@0
|
205 ]));
|
Chris@0
|
206 }
|
Chris@0
|
207 }
|
Chris@0
|
208
|
Chris@0
|
209 return $message;
|
Chris@0
|
210 }
|
Chris@0
|
211
|
Chris@0
|
212 /**
|
Chris@0
|
213 * {@inheritdoc}
|
Chris@0
|
214 */
|
Chris@0
|
215 public function save(array $form, FormStateInterface $form_state) {
|
Chris@0
|
216 $message = $this->entity;
|
Chris@0
|
217 $user = $this->currentUser();
|
Chris@0
|
218 // Save the message. In core this is a no-op but should contrib wish to
|
Chris@0
|
219 // implement message storage, this will make the task of swapping in a real
|
Chris@0
|
220 // storage controller straight-forward.
|
Chris@0
|
221 $message->save();
|
Chris@0
|
222 $this->mailHandler->sendMailMessages($message, $user);
|
Chris@0
|
223 $contact_form = $message->getContactForm();
|
Chris@0
|
224
|
Chris@0
|
225 $this->flood->register('contact', $this->config('contact.settings')->get('flood.interval'));
|
Chris@0
|
226 if ($submission_message = $contact_form->getMessage()) {
|
Chris@17
|
227 $this->messenger()->addStatus($submission_message);
|
Chris@0
|
228 }
|
Chris@0
|
229
|
Chris@0
|
230 // To avoid false error messages caused by flood control, redirect away from
|
Chris@0
|
231 // the contact form; either to the contacted user account or the front page.
|
Chris@0
|
232 if ($message->isPersonal() && $user->hasPermission('access user profiles')) {
|
Chris@18
|
233 $form_state->setRedirectUrl($message->getPersonalRecipient()->toUrl());
|
Chris@0
|
234 }
|
Chris@0
|
235 else {
|
Chris@0
|
236 $form_state->setRedirectUrl($contact_form->getRedirectUrl());
|
Chris@0
|
237 }
|
Chris@0
|
238 }
|
Chris@0
|
239
|
Chris@0
|
240 }
|