You are here

public function ForwardEmailForm::submitForm in Social media share 8

Form submission handler.

Parameters

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

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

Overrides FormInterface::submitForm

File

src/Form/ForwardEmailForm.php, line 135

Class

ForwardEmailForm
Class ForwardEmailForm.

Namespace

Drupal\social_media\Form

Code

public function submitForm(array &$form, FormStateInterface $form_state) {
  $recipient = $form_state
    ->getValue('email');
  $params['message'] = $form_state
    ->getValue('body');
  $params['subject'] = $form_state
    ->getValue('subject');
  $langcode = $this->languageManager
    ->getCurrentLanguage()
    ->getId();
  $result = $this->mailManager
    ->mail('social_media', 'forward_email', $recipient, $langcode, $params, NULL, TRUE);
  if ($result['result'] !== TRUE) {
    $this->logger
      ->notice('Sent email to %recipient', [
      '%recipient' => $recipient,
    ]);
    $this
      ->messenger()
      ->addError($this
      ->t('There was a problem sending your message and it was not sent.'));
  }
  else {
    $this->logger
      ->notice('Sent email to %recipient', [
      '%recipient' => $recipient,
    ]);
    $this
      ->messenger()
      ->addMessage($this
      ->t('Your message has been send to @email', [
      '@email' => $recipient,
    ]));
  }
}