You are here

public function MessageForm::save in Zircon Profile 8.0

Same name and namespace in other branches
  1. 8 core/modules/contact/src/MessageForm.php \Drupal\contact\MessageForm::save()

Form submission handler for the 'save' action.

Normally this method should be overridden to provide specific messages to the user and redirect the form after the entity has been saved.

Parameters

array $form: An associative array containing the structure of the form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

Return value

int Either SAVED_NEW or SAVED_UPDATED, depending on the operation performed.

Overrides EntityForm::save

File

core/modules/contact/src/MessageForm.php, line 210
Contains \Drupal\contact\MessageForm.

Class

MessageForm
Form controller for contact message forms.

Namespace

Drupal\contact

Code

public function save(array $form, FormStateInterface $form_state) {
  $message = $this->entity;
  $user = $this
    ->currentUser();
  $this->mailHandler
    ->sendMailMessages($message, $user);
  $this->flood
    ->register('contact', $this
    ->config('contact.settings')
    ->get('flood.interval'));
  drupal_set_message($this
    ->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
    ->isPersonal() && $user
    ->hasPermission('access user profiles')) {
    $form_state
      ->setRedirectUrl($message
      ->getPersonalRecipient()
      ->urlInfo());
  }
  else {
    $form_state
      ->setRedirect('<front>');
  }

  // 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();
}