MessageForm.php 6.82 KB
Newer Older
1 2 3 4
<?php

/**
 * @file
5
 * Contains \Drupal\contact\MessageForm.
6 7 8 9
 */

namespace Drupal\contact;

10
use Drupal\Component\Utility\String;
11
use Drupal\Core\Entity\ContentEntityForm;
12 13
use Drupal\Core\Entity\EntityManagerInterface;
use Drupal\Core\Flood\FloodInterface;
14
use Drupal\Core\Form\FormStateInterface;
15 16 17
use Drupal\Core\Language\Language;
use Drupal\Core\Language\LanguageInterface;
use Drupal\Core\Language\LanguageManagerInterface;
18
use Symfony\Component\DependencyInjection\ContainerInterface;
19 20 21 22

/**
 * Form controller for contact message forms.
 */
23
class MessageForm extends ContentEntityForm {
24

25 26 27 28 29 30 31
  /**
   * The message being used by this form.
   *
   * @var \Drupal\contact\MessageInterface
   */
  protected $entity;

32 33 34 35 36 37 38
  /**
   * The flood control mechanism.
   *
   * @var \Drupal\Core\Flood\FloodInterface
   */
  protected $flood;

39 40 41 42 43 44 45
  /**
   * The language manager service.
   *
   * @var \Drupal\Core\Language\LanguageManagerInterface
   */
  protected $languageManager;

46 47 48 49 50 51 52
  /**
   * The contact mail handler service.
   *
   * @var \Drupal\contact\MailHandlerInterface
   */
  protected $mailHandler;

53
  /**
54
   * Constructs a MessageForm object.
55 56 57 58 59
   *
   * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
   *   The entity manager.
   * @param \Drupal\Core\Flood\FloodInterface $flood
   *   The flood control mechanism.
60 61
   * @param \Drupal\Core\Language\LanguageManagerInterface $language_manager
   *   The language manager service.
62 63
   * @param \Drupal\contact\MailHandlerInterface $mail_handler
   *   The contact mail handler service.
64
   */
65
  public function __construct(EntityManagerInterface $entity_manager, FloodInterface $flood, LanguageManagerInterface $language_manager, MailHandlerInterface $mail_handler) {
66 67
    parent::__construct($entity_manager);
    $this->flood = $flood;
68
    $this->languageManager = $language_manager;
69
    $this->mailHandler = $mail_handler;
70 71 72 73 74 75 76 77
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static(
      $container->get('entity.manager'),
78
      $container->get('flood'),
79 80
      $container->get('language_manager'),
      $container->get('contact.mail_handler')
81 82 83
    );
  }

84
  /**
85
   * {@inheritdoc}
86
   */
87
  public function form(array $form, FormStateInterface $form_state) {
88
    $user = $this->currentUser();
89
    $message = $this->entity;
90 91 92 93 94 95 96 97
    $form = parent::form($form, $form_state, $message);
    $form['#attributes']['class'][] = 'contact-form';

    if (!empty($message->preview)) {
      $form['preview'] = array(
        '#theme_wrappers' => array('container__preview'),
        '#attributes' => array('class' => array('preview')),
      );
98
      $form['preview']['message'] = $this->entityManager->getViewBuilder('contact_message')->view($message, 'full');
99 100
    }

101
    $form['langcode'] = array(
102
      '#title' => $this->t('Language'),
103
      '#type' => 'language_select',
104
      '#default_value' => $message->getUntranslated()->language()->getId(),
105
      '#languages' => Language::STATE_ALL,
106 107
      // Language module may expose or hide this element, see language_form_alter().
      '#access' => FALSE,
108 109
    );

110 111
    $form['name'] = array(
      '#type' => 'textfield',
112
      '#title' => $this->t('Your name'),
113 114 115 116 117
      '#maxlength' => 255,
      '#required' => TRUE,
    );
    $form['mail'] = array(
      '#type' => 'email',
118
      '#title' => $this->t('Your email address'),
119 120
      '#required' => TRUE,
    );
121
    if ($user->isAnonymous()) {
122 123
      $form['#attached']['library'][] = 'core/drupal.form';
      $form['#attributes']['data-user-info-from-browser'] = TRUE;
124
    }
125
    // Do not allow authenticated users to alter the name or email values to
126 127 128
    // prevent the impersonation of other users.
    else {
      $form['name']['#type'] = 'item';
129
      $form['name']['#value'] = $user->getUsername();
130
      $form['name']['#required'] = FALSE;
131
      $form['name']['#markup'] = String::checkPlain($user->getUsername());
132 133

      $form['mail']['#type'] = 'item';
134
      $form['mail']['#value'] = $user->getEmail();
135
      $form['mail']['#required'] = FALSE;
136
      $form['mail']['#markup'] = String::checkPlain($user->getEmail());
137 138
    }

139 140
    // The user contact form has a preset recipient.
    if ($message->isPersonal()) {
141 142
      $form['recipient'] = array(
        '#type' => 'item',
143
        '#title' => $this->t('To'),
144
        '#value' => $message->getPersonalRecipient()->id(),
145 146
        'name' => array(
          '#theme' => 'username',
147
          '#account' => $message->getPersonalRecipient(),
148 149 150 151 152 153
        ),
      );
    }

    $form['copy'] = array(
      '#type' => 'checkbox',
154
      '#title' => $this->t('Send yourself a copy'),
155 156
      // Do not allow anonymous users to send themselves a copy, because it can
      // be abused to spam people.
157
      '#access' => $user->isAuthenticated(),
158 159 160 161 162
    );
    return $form;
  }

  /**
163
   * {@inheritdoc}
164
   */
165
  public function actions(array $form, FormStateInterface $form_state) {
166
    $elements = parent::actions($form, $form_state);
167
    $elements['submit']['#value'] = $this->t('Send message');
168
    $elements['preview'] = array(
169
      '#value' => $this->t('Preview'),
170
      '#validate' => array('::validate'),
171
      '#submit' => array('::submitForm', '::preview'),
172 173 174 175 176 177 178
    );
    return $elements;
  }

  /**
   * Form submission handler for the 'preview' action.
   */
179
  public function preview(array $form, FormStateInterface $form_state) {
180
    $message = $this->entity;
181
    $message->preview = TRUE;
182
    $form_state->setRebuild();
183 184 185
  }

  /**
186
   * {@inheritdoc}
187
   */
188
  public function save(array $form, FormStateInterface $form_state) {
189
    $message = $this->entity;
190 191
    $user = $this->currentUser();
    $this->mailHandler->sendMailMessages($message, $user);
192

193 194
    $this->flood->register('contact', $this->config('contact.settings')->get('flood.interval'));
    drupal_set_message($this->t('Your message has been sent.'));
195 196 197

    // To avoid false error messages caused by flood control, redirect away from
    // the contact form; either to the contacted user account or the front page.
198
    if ($message->isPersonal() && $user->hasPermission('access user profiles')) {
199
      $form_state->setRedirectUrl($message->getPersonalRecipient()->urlInfo());
200 201
    }
    else {
202
      $form_state->setRedirect('<front>');
203
    }
204 205 206 207
    // Save the message. In core this is a no-op but should contrib wish to
    // implement message storage, this will make the task of swapping in a real
    // storage controller straight-forward.
    $message->save();
208
  }
209 210 211 212

  /**
   * {@inheritdoc}
   */
213
  protected function init(FormStateInterface $form_state) {
214 215 216 217 218 219
    $message = $this->entity;

    // Make the message inherit the current content language unless specifically
    // set.
    if ($message->isNew() && !$message->langcode->value) {
      $language_content = $this->languageManager->getCurrentLanguage(LanguageInterface::TYPE_CONTENT);
220
      $message->langcode->value = $language_content->getId();
221 222 223 224 225
    }

    parent::init($form_state);
  }

226
}