Chris@0: flood = $flood; Chris@0: $this->languageManager = $language_manager; Chris@0: $this->mailHandler = $mail_handler; Chris@0: $this->dateFormatter = $date_formatter; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public static function create(ContainerInterface $container) { Chris@0: return new static( Chris@17: $container->get('entity.repository'), Chris@0: $container->get('flood'), Chris@0: $container->get('language_manager'), Chris@0: $container->get('contact.mail_handler'), Chris@0: $container->get('date.formatter'), Chris@0: $container->get('entity_type.bundle.info'), Chris@0: $container->get('datetime.time') Chris@0: ); Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function form(array $form, FormStateInterface $form_state) { Chris@0: $user = $this->currentUser(); Chris@0: $message = $this->entity; Chris@0: $form = parent::form($form, $form_state, $message); Chris@0: $form['#attributes']['class'][] = 'contact-form'; Chris@0: Chris@0: if (!empty($message->preview)) { Chris@0: $form['preview'] = [ Chris@0: '#theme_wrappers' => ['container__preview'], Chris@0: '#attributes' => ['class' => ['preview']], Chris@0: ]; Chris@17: $form['preview']['message'] = $this->entityTypeManager->getViewBuilder('contact_message')->view($message, 'full'); Chris@0: } Chris@0: Chris@0: $form['name'] = [ Chris@0: '#type' => 'textfield', Chris@0: '#title' => $this->t('Your name'), Chris@0: '#maxlength' => 255, Chris@0: '#required' => TRUE, Chris@0: ]; Chris@0: $form['mail'] = [ Chris@0: '#type' => 'email', Chris@0: '#title' => $this->t('Your email address'), Chris@0: '#required' => TRUE, Chris@0: ]; Chris@0: if ($user->isAnonymous()) { Chris@0: $form['#attached']['library'][] = 'core/drupal.form'; Chris@0: $form['#attributes']['data-user-info-from-browser'] = TRUE; Chris@0: } Chris@0: // Do not allow authenticated users to alter the name or email values to Chris@0: // prevent the impersonation of other users. Chris@0: else { Chris@0: $form['name']['#type'] = 'item'; Chris@0: $form['name']['#value'] = $user->getDisplayName(); Chris@0: $form['name']['#required'] = FALSE; Chris@0: $form['name']['#plain_text'] = $user->getDisplayName(); Chris@0: Chris@0: $form['mail']['#type'] = 'item'; Chris@0: $form['mail']['#value'] = $user->getEmail(); Chris@0: $form['mail']['#required'] = FALSE; Chris@0: $form['mail']['#plain_text'] = $user->getEmail(); Chris@0: } Chris@0: Chris@0: // The user contact form has a preset recipient. Chris@0: if ($message->isPersonal()) { Chris@0: $form['recipient'] = [ Chris@0: '#type' => 'item', Chris@0: '#title' => $this->t('To'), Chris@0: '#value' => $message->getPersonalRecipient()->id(), Chris@0: 'name' => [ Chris@0: '#theme' => 'username', Chris@0: '#account' => $message->getPersonalRecipient(), Chris@0: ], Chris@0: ]; Chris@0: } Chris@0: Chris@0: $form['copy'] = [ Chris@0: '#type' => 'checkbox', Chris@0: '#title' => $this->t('Send yourself a copy'), Chris@0: // Do not allow anonymous users to send themselves a copy, because it can Chris@0: // be abused to spam people. Chris@0: '#access' => $user->isAuthenticated(), Chris@0: ]; Chris@0: return $form; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function actions(array $form, FormStateInterface $form_state) { Chris@0: $elements = parent::actions($form, $form_state); Chris@0: $elements['submit']['#value'] = $this->t('Send message'); Chris@0: $elements['preview'] = [ Chris@0: '#type' => 'submit', Chris@0: '#value' => $this->t('Preview'), Chris@0: '#submit' => ['::submitForm', '::preview'], Chris@0: ]; Chris@0: return $elements; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Form submission handler for the 'preview' action. Chris@0: */ Chris@0: public function preview(array $form, FormStateInterface $form_state) { Chris@0: $message = $this->entity; Chris@0: $message->preview = TRUE; Chris@0: $form_state->setRebuild(); Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function validateForm(array &$form, FormStateInterface $form_state) { Chris@0: $message = parent::validateForm($form, $form_state); Chris@0: Chris@0: // Check if flood control has been activated for sending emails. Chris@0: if (!$this->currentUser()->hasPermission('administer contact forms') && (!$message->isPersonal() || !$this->currentUser()->hasPermission('administer users'))) { Chris@0: $limit = $this->config('contact.settings')->get('flood.limit'); Chris@0: $interval = $this->config('contact.settings')->get('flood.interval'); Chris@0: Chris@0: if (!$this->flood->isAllowed('contact', $limit, $interval)) { Chris@0: $form_state->setErrorByName('', $this->t('You cannot send more than %limit messages in @interval. Try again later.', [ Chris@0: '%limit' => $limit, Chris@0: '@interval' => $this->dateFormatter->formatInterval($interval), Chris@0: ])); Chris@0: } Chris@0: } Chris@0: Chris@0: return $message; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function save(array $form, FormStateInterface $form_state) { Chris@0: $message = $this->entity; Chris@0: $user = $this->currentUser(); Chris@0: // Save the message. In core this is a no-op but should contrib wish to Chris@0: // implement message storage, this will make the task of swapping in a real Chris@0: // storage controller straight-forward. Chris@0: $message->save(); Chris@0: $this->mailHandler->sendMailMessages($message, $user); Chris@0: $contact_form = $message->getContactForm(); Chris@0: Chris@0: $this->flood->register('contact', $this->config('contact.settings')->get('flood.interval')); Chris@0: if ($submission_message = $contact_form->getMessage()) { Chris@17: $this->messenger()->addStatus($submission_message); Chris@0: } Chris@0: Chris@0: // To avoid false error messages caused by flood control, redirect away from Chris@0: // the contact form; either to the contacted user account or the front page. Chris@0: if ($message->isPersonal() && $user->hasPermission('access user profiles')) { Chris@18: $form_state->setRedirectUrl($message->getPersonalRecipient()->toUrl()); Chris@0: } Chris@0: else { Chris@0: $form_state->setRedirectUrl($contact_form->getRedirectUrl()); Chris@0: } Chris@0: } Chris@0: Chris@0: }