3 namespace Drupal\contact;
5 use Drupal\Component\Datetime\TimeInterface;
6 use Drupal\Core\Datetime\DateFormatterInterface;
7 use Drupal\Core\Entity\ContentEntityForm;
8 use Drupal\Core\Entity\EntityManagerInterface;
9 use Drupal\Core\Entity\EntityTypeBundleInfoInterface;
10 use Drupal\Core\Flood\FloodInterface;
11 use Drupal\Core\Form\FormStateInterface;
12 use Drupal\Core\Language\LanguageManagerInterface;
13 use Symfony\Component\DependencyInjection\ContainerInterface;
16 * Form controller for contact message forms.
20 class MessageForm extends ContentEntityForm {
23 * The message being used by this form.
25 * @var \Drupal\contact\MessageInterface
30 * The flood control mechanism.
32 * @var \Drupal\Core\Flood\FloodInterface
37 * The language manager service.
39 * @var \Drupal\Core\Language\LanguageManagerInterface
41 protected $languageManager;
44 * The contact mail handler service.
46 * @var \Drupal\contact\MailHandlerInterface
48 protected $mailHandler;
51 * The date formatter service.
53 * @var \Drupal\Core\Datetime\DateFormatterInterface
55 protected $dateFormatter;
58 * Constructs a MessageForm object.
60 * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
62 * @param \Drupal\Core\Flood\FloodInterface $flood
63 * The flood control mechanism.
64 * @param \Drupal\Core\Language\LanguageManagerInterface $language_manager
65 * The language manager service.
66 * @param \Drupal\contact\MailHandlerInterface $mail_handler
67 * The contact mail handler service.
68 * @param \Drupal\Core\Datetime\DateFormatterInterface $date_formatter
70 * @param \Drupal\Core\Entity\EntityTypeBundleInfoInterface $entity_type_bundle_info
71 * The entity type bundle service.
72 * @param \Drupal\Component\Datetime\TimeInterface $time
75 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) {
76 parent::__construct($entity_manager, $entity_type_bundle_info, $time);
77 $this->flood = $flood;
78 $this->languageManager = $language_manager;
79 $this->mailHandler = $mail_handler;
80 $this->dateFormatter = $date_formatter;
86 public static function create(ContainerInterface $container) {
88 $container->get('entity.manager'),
89 $container->get('flood'),
90 $container->get('language_manager'),
91 $container->get('contact.mail_handler'),
92 $container->get('date.formatter'),
93 $container->get('entity_type.bundle.info'),
94 $container->get('datetime.time')
101 public function form(array $form, FormStateInterface $form_state) {
102 $user = $this->currentUser();
103 $message = $this->entity;
104 $form = parent::form($form, $form_state, $message);
105 $form['#attributes']['class'][] = 'contact-form';
107 if (!empty($message->preview)) {
109 '#theme_wrappers' => ['container__preview'],
110 '#attributes' => ['class' => ['preview']],
112 $form['preview']['message'] = $this->entityManager->getViewBuilder('contact_message')->view($message, 'full');
116 '#type' => 'textfield',
117 '#title' => $this->t('Your name'),
123 '#title' => $this->t('Your email address'),
126 if ($user->isAnonymous()) {
127 $form['#attached']['library'][] = 'core/drupal.form';
128 $form['#attributes']['data-user-info-from-browser'] = TRUE;
130 // Do not allow authenticated users to alter the name or email values to
131 // prevent the impersonation of other users.
133 $form['name']['#type'] = 'item';
134 $form['name']['#value'] = $user->getDisplayName();
135 $form['name']['#required'] = FALSE;
136 $form['name']['#plain_text'] = $user->getDisplayName();
138 $form['mail']['#type'] = 'item';
139 $form['mail']['#value'] = $user->getEmail();
140 $form['mail']['#required'] = FALSE;
141 $form['mail']['#plain_text'] = $user->getEmail();
144 // The user contact form has a preset recipient.
145 if ($message->isPersonal()) {
146 $form['recipient'] = [
148 '#title' => $this->t('To'),
149 '#value' => $message->getPersonalRecipient()->id(),
151 '#theme' => 'username',
152 '#account' => $message->getPersonalRecipient(),
158 '#type' => 'checkbox',
159 '#title' => $this->t('Send yourself a copy'),
160 // Do not allow anonymous users to send themselves a copy, because it can
161 // be abused to spam people.
162 '#access' => $user->isAuthenticated(),
170 public function actions(array $form, FormStateInterface $form_state) {
171 $elements = parent::actions($form, $form_state);
172 $elements['submit']['#value'] = $this->t('Send message');
173 $elements['preview'] = [
175 '#value' => $this->t('Preview'),
176 '#submit' => ['::submitForm', '::preview'],
182 * Form submission handler for the 'preview' action.
184 public function preview(array $form, FormStateInterface $form_state) {
185 $message = $this->entity;
186 $message->preview = TRUE;
187 $form_state->setRebuild();
193 public function validateForm(array &$form, FormStateInterface $form_state) {
194 $message = parent::validateForm($form, $form_state);
196 // Check if flood control has been activated for sending emails.
197 if (!$this->currentUser()->hasPermission('administer contact forms') && (!$message->isPersonal() || !$this->currentUser()->hasPermission('administer users'))) {
198 $limit = $this->config('contact.settings')->get('flood.limit');
199 $interval = $this->config('contact.settings')->get('flood.interval');
201 if (!$this->flood->isAllowed('contact', $limit, $interval)) {
202 $form_state->setErrorByName('', $this->t('You cannot send more than %limit messages in @interval. Try again later.', [
204 '@interval' => $this->dateFormatter->formatInterval($interval),
215 public function save(array $form, FormStateInterface $form_state) {
216 $message = $this->entity;
217 $user = $this->currentUser();
218 // Save the message. In core this is a no-op but should contrib wish to
219 // implement message storage, this will make the task of swapping in a real
220 // storage controller straight-forward.
222 $this->mailHandler->sendMailMessages($message, $user);
223 $contact_form = $message->getContactForm();
225 $this->flood->register('contact', $this->config('contact.settings')->get('flood.interval'));
226 if ($submission_message = $contact_form->getMessage()) {
227 drupal_set_message($submission_message);
230 // To avoid false error messages caused by flood control, redirect away from
231 // the contact form; either to the contacted user account or the front page.
232 if ($message->isPersonal() && $user->hasPermission('access user profiles')) {
233 $form_state->setRedirectUrl($message->getPersonalRecipient()->urlInfo());
236 $form_state->setRedirectUrl($contact_form->getRedirectUrl());