class MessageFormController

Form controller for contact message forms.

Hierarchy

Expanded class hierarchy of MessageFormController

File

drupal/core/modules/contact/lib/Drupal/contact/MessageFormController.php, line 17
Definition of Drupal\contact\MessageFormController.

Namespace

Drupal\contact
View source
class MessageFormController extends EntityFormController {

  /**
   * Overrides Drupal\Core\Entity\EntityFormController::form().
   */
  public function form(array $form, array &$form_state) {
    global $user;
    $message = $this->entity;
    $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',
          ),
        ),
      );
      $form['preview']['message'] = entity_view($message, 'full');
    }
    $form['name'] = array(
      '#type' => 'textfield',
      '#title' => t('Your name'),
      '#maxlength' => 255,
      '#required' => TRUE,
    );
    $form['mail'] = array(
      '#type' => 'email',
      '#title' => t('Your e-mail address'),
      '#required' => TRUE,
    );
    if (!$user->uid) {
      $form['#attached']['library'][] = array(
        'system',
        'jquery.cookie',
      );
      $form['#attributes']['class'][] = 'user-info-from-cookie';
    }
    else {
      $form['name']['#type'] = 'item';
      $form['name']['#value'] = $user->name;
      $form['name']['#required'] = FALSE;
      $form['name']['#markup'] = check_plain(user_format_name($user));
      $form['mail']['#type'] = 'item';
      $form['mail']['#value'] = $user->mail;
      $form['mail']['#required'] = FALSE;
      $form['mail']['#markup'] = check_plain($user->mail);
    }

    // The user contact form has a preset recipient.
    if ($message
      ->isPersonal()) {
      $form['recipient'] = array(
        '#type' => 'item',
        '#title' => t('To'),
        '#value' => $message->recipient,
        'name' => array(
          '#theme' => 'username',
          '#account' => $message->recipient,
        ),
      );
    }
    else {
      $form['category'] = array(
        '#type' => 'value',
        '#value' => $message->category,
      );
    }
    $form['subject'] = array(
      '#type' => 'textfield',
      '#title' => t('Subject'),
      '#maxlength' => 100,
      '#required' => TRUE,
    );
    $form['message'] = array(
      '#type' => 'textarea',
      '#title' => t('Message'),
      '#required' => TRUE,
      '#rows' => 12,
    );
    $form['copy'] = array(
      '#type' => 'checkbox',
      '#title' => t('Send yourself a copy.'),
      // Do not allow anonymous users to send themselves a copy, because it can
      // be abused to spam people.
      '#access' => !empty($user->uid),
    );
    return $form;
  }

  /**
   * Overrides Drupal\Core\Entity\EntityFormController::actions().
   */
  public function actions(array $form, array &$form_state) {
    $elements = parent::actions($form, $form_state);
    $elements['submit']['#value'] = t('Send message');
    $elements['delete']['#access'] = FALSE;
    $elements['preview'] = array(
      '#value' => t('Preview'),
      '#validate' => array(
        array(
          $this,
          'validate',
        ),
      ),
      '#submit' => array(
        array(
          $this,
          'submit',
        ),
        array(
          $this,
          'preview',
        ),
      ),
    );
    return $elements;
  }

  /**
   * Form submission handler for the 'preview' action.
   */
  public function preview(array $form, array &$form_state) {
    $message = $this->entity;
    $message->preview = TRUE;
    $form_state['rebuild'] = TRUE;
  }

  /**
   * Overrides Drupal\Core\Entity\EntityFormController::save().
   */
  public function save(array $form, array &$form_state) {
    global $user;
    $language_interface = language(Language::TYPE_INTERFACE);
    $message = $this->entity;
    $sender = clone user_load($user->uid);
    if (!$user->uid) {

      // At this point, $sender contains drupal_anonymous_user(), so we need to
      // take over the submitted form values.
      $sender->name = $message->name;
      $sender->mail = $message->mail;

      // Save the anonymous user information to a cookie for reuse.
      user_cookie_save(array(
        'name' => $message->name,
        'mail' => $message->mail,
      ));

      // For the e-mail message, clarify that the sender name is not verified; it
      // could potentially clash with a username on this site.
      $sender->name = t('!name (not verified)', array(
        '!name' => $message->name,
      ));
    }

    // Build e-mail parameters.
    $params['contact_message'] = $message;
    $params['sender'] = $sender;
    if (!$message
      ->isPersonal()) {

      // Send to the category recipient(s), using the site's default language.
      $category = entity_load('contact_category', $message->category);
      $params['contact_category'] = $category;
      $to = implode(', ', $category->recipients);
      $recipient_langcode = language_default()->langcode;
    }
    elseif ($message->recipient instanceof UserInterface) {

      // Send to the user in the user's preferred language.
      $to = $message->recipient->mail;
      $recipient_langcode = user_preferred_langcode($message->recipient);
    }
    else {
      throw new \RuntimeException(t('Unable to determine message recipient.'));
    }

    // Send e-mail to the recipient(s).
    drupal_mail('contact', 'page_mail', $to, $recipient_langcode, $params, $sender->mail);

    // If requested, send a copy to the user, using the current language.
    if ($message->copy) {
      drupal_mail('contact', 'page_copy', $sender->mail, $language_interface->langcode, $params, $sender->mail);
    }

    // If configured, send an auto-reply, using the current language.
    if (!$message
      ->isPersonal() && $category->reply) {

      // User contact forms do not support an auto-reply message, so this
      // message always originates from the site.
      drupal_mail('contact', 'page_autoreply', $sender->mail, $language_interface->langcode, $params);
    }
    \Drupal::service('flood')
      ->register('contact', config('contact.settings')
      ->get('flood.interval'));
    if (!$message
      ->isPersonal()) {
      watchdog('contact', '%sender-name (@sender-from) sent an e-mail regarding %category.', array(
        '%sender-name' => $sender->name,
        '@sender-from' => $sender->mail,
        '%category' => $category
          ->label(),
      ));
    }
    else {
      watchdog('contact', '%sender-name (@sender-from) sent %recipient-name an e-mail.', array(
        '%sender-name' => $sender->name,
        '@sender-from' => $sender->mail,
        '%recipient-name' => $message->recipient->name,
      ));
    }
    drupal_set_message(t('Your message has been sent.'));

    // 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.
    if ($message->recipient instanceof UserInterface && user_access('access user profiles')) {
      $uri = $message->recipient
        ->uri();
      $form_state['redirect'] = array(
        $uri['path'],
        $uri['options'],
      );
    }
    else {
      $form_state['redirect'] = '';
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
EntityFormController::$entity protected property The entity being used by this form.
EntityFormController::$operation protected property The name of the current operation.
EntityFormController::actionsElement protected function Returns the action form element for the current entity form.
EntityFormController::buildEntity public function Implements \Drupal\Core\Entity\EntityFormControllerInterface::buildEntity(). Overrides EntityFormControllerInterface::buildEntity 2
EntityFormController::buildForm public function Form constructor. Overrides FormInterface::buildForm 1
EntityFormController::delete public function Form submission handler for the 'delete' action. 12
EntityFormController::getBaseFormID public function Returns a string identifying the base form. Overrides BaseFormIdInterface::getBaseFormID
EntityFormController::getEntity public function Implements \Drupal\Core\Entity\EntityFormControllerInterface::getEntity(). Overrides EntityFormControllerInterface::getEntity
EntityFormController::getFormDisplay public function Returns the form display. Overrides EntityFormControllerInterface::getFormDisplay
EntityFormController::getFormID public function Returns a unique string identifying the form. Overrides FormInterface::getFormID
EntityFormController::getFormLangcode public function Implements \Drupal\Core\Entity\EntityFormControllerInterface::getFormLangcode(). Overrides EntityFormControllerInterface::getFormLangcode
EntityFormController::getOperation public function Implements \Drupal\Core\Entity\EntityFormControllerInterface::getOperation(). Overrides EntityFormControllerInterface::getOperation
EntityFormController::init protected function Initialize the form state and the entity before the first form build. 1
EntityFormController::isDefaultFormLangcode public function Implements \Drupal\Core\Entity\EntityFormControllerInterface::isDefaultFormLangcode(). Overrides EntityFormControllerInterface::isDefaultFormLangcode
EntityFormController::prepareEntity protected function Prepares the entity object before the form is built first. 3
EntityFormController::setEntity public function Implements \Drupal\Core\Entity\EntityFormControllerInterface::setEntity(). Overrides EntityFormControllerInterface::setEntity
EntityFormController::setFormDisplay public function Sets the form display. Overrides EntityFormControllerInterface::setFormDisplay
EntityFormController::setOperation public function Sets the operation for this form.
EntityFormController::submit public function Implements \Drupal\Core\Entity\EntityFormControllerInterface::submit(). Overrides EntityFormControllerInterface::submit 13
EntityFormController::submitEntityLanguage protected function Handle possible entity language changes. 1
EntityFormController::submitForm public function Form submission handler. Overrides FormInterface::submitForm
EntityFormController::updateFormLangcode protected function Updates the form language to reflect any change to the entity language.
EntityFormController::validate public function Implements \Drupal\Core\Entity\EntityFormControllerInterface::validate(). Overrides EntityFormControllerInterface::validate 11
EntityFormController::validateForm public function Form validation handler. Overrides FormInterface::validateForm
EntityFormController::__construct public function Constructs an EntityFormController object. 5
MessageFormController::actions public function Overrides Drupal\Core\Entity\EntityFormController::actions(). Overrides EntityFormController::actions
MessageFormController::form public function Overrides Drupal\Core\Entity\EntityFormController::form(). Overrides EntityFormController::form
MessageFormController::preview public function Form submission handler for the 'preview' action.
MessageFormController::save public function Overrides Drupal\Core\Entity\EntityFormController::save(). Overrides EntityFormController::save