public function MessageFormController::save

Overrides Drupal\Core\Entity\EntityFormController::save().

Overrides EntityFormController::save

File

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

Class

MessageFormController
Form controller for contact message forms.

Namespace

Drupal\contact

Code

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'] = '';
  }
}