function MessageForm::save

Same name in other branches
  1. 9 core/modules/contact/src/MessageForm.php \Drupal\contact\MessageForm::save()
  2. 10 core/modules/contact/src/MessageForm.php \Drupal\contact\MessageForm::save()
  3. 11.x core/modules/contact/src/MessageForm.php \Drupal\contact\MessageForm::save()

Overrides EntityForm::save

File

core/modules/contact/src/MessageForm.php, line 215

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();
    // 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();
    $this->mailHandler
        ->sendMailMessages($message, $user);
    $contact_form = $message->getContactForm();
    $this->flood
        ->register('contact', $this->config('contact.settings')
        ->get('flood.interval'));
    if ($submission_message = $contact_form->getMessage()) {
        $this->messenger()
            ->addStatus($submission_message);
    }
    // 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()
            ->toUrl());
    }
    else {
        $form_state->setRedirectUrl($contact_form->getRedirectUrl());
    }
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.