You are here

public function SendGridTestForm::submitForm in SendGrid Integration 8.2

Same name and namespace in other branches
  1. 8 src/Form/SendGridTestForm.php \Drupal\sendgrid_integration\Form\SendGridTestForm::submitForm()

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/SendGridTestForm.php, line 136

Class

SendGridTestForm
Class SendGridSettingsForm.

Namespace

Drupal\sendgrid_integration\Form

Code

public function submitForm(array &$form, FormStateInterface $form_state) {
  $config = $this
    ->configFactory()
    ->getEditable('sendgrid_integration.settings');
  $site_settings = $this
    ->config('system.site');
  $config
    ->set('test_defaults.to', $form_state
    ->getValue('to'));
  $config
    ->set('test_defaults.subject', $form_state
    ->getValue('subject'));
  $config
    ->set('test_defaults.body.value', $form_state
    ->getValue([
    'body',
    'value',
  ]));
  $config
    ->set('test_defaults.body.format', $form_state
    ->getValue([
    'body',
    'format',
  ]));
  $config
    ->set('test_defaults.from_name', $form_state
    ->getValue('from_name'));
  $config
    ->set('test_defaults.to_name', $form_state
    ->getValue('to_name'));
  $config
    ->set('test_defaults.reply_to', $form_state
    ->getValue('reply_to'));
  $config
    ->save();
  $params = $config
    ->get('test_defaults');
  $params['include_test_attachment'] = $form_state
    ->getValue('include_attachment');
  $params['body'] = check_markup($params['body']['value'], $params['body']['format']);

  // Attempt to send the email and post a message if it was successful.
  if ($config
    ->get('test_defaults.from_name')) {
    $from = $config
      ->get('test_defaults.from_name') . ' <' . $site_settings
      ->get('mail') . '>';
  }
  else {
    $from = $site_settings
      ->get('mail');
  }
  $result = $this->mailManager
    ->mail('sendgrid_integration', 'sengrid_integration_troubleshooting_test', $config
    ->get('test_defaults.to'), $this->languageManager
    ->getDefaultLanguage()
    ->getId(), $params, $from);
  if (isset($result['result']) && $result['result'] == TRUE) {
    $this->messenger
      ->addMessage($this
      ->t('SendGrid test email sent from %from to %to.', [
      '%from' => $from,
      '%to' => $config
        ->get('test_defaults.to'),
    ]));
  }
}