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