You are here

public function SettingsForm::submitForm in Swift Mailer 8

Same name and namespace in other branches
  1. 8.2 src/Form/SettingsForm.php \Drupal\swiftmailer\Form\SettingsForm::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 ConfigFormBase::submitForm

File

src/Form/SettingsForm.php, line 314

Class

SettingsForm
Swift Mailer settings form.

Namespace

Drupal\swiftmailer\Form

Code

public function submitForm(array &$form, FormStateInterface $form_state) {
  $config = $this
    ->config('swiftmailer.transport');
  if ($form_state
    ->hasValue([
    'transport',
    'type',
  ])) {
    $config
      ->set('transport', $form_state
      ->getValue([
      'transport',
      'type',
    ]));
    $messenger = \Drupal::messenger();
    switch ($form_state
      ->getValue([
      'transport',
      'type',
    ])) {
      case SWIFTMAILER_TRANSPORT_SMTP:
        $config
          ->set('smtp_host', $form_state
          ->getValue([
          'transport',
          'configuration',
          SWIFTMAILER_TRANSPORT_SMTP,
          'server',
        ]));
        $config
          ->set('smtp_port', $form_state
          ->getValue([
          'transport',
          'configuration',
          SWIFTMAILER_TRANSPORT_SMTP,
          'port',
        ]));
        $config
          ->set('smtp_encryption', $form_state
          ->getValue([
          'transport',
          'configuration',
          SWIFTMAILER_TRANSPORT_SMTP,
          'encryption',
        ]));
        $config
          ->set('smtp_credential_provider', $form_state
          ->getValue([
          'transport',
          'configuration',
          SWIFTMAILER_TRANSPORT_SMTP,
          'credential_provider',
        ]));
        $config
          ->set('smtp_credentials', [
          $config
            ->get('smtp_credential_provider') => $form_state
            ->getValue([
            'transport',
            'configuration',
            SWIFTMAILER_TRANSPORT_SMTP,
            'credentials',
            $config
              ->get('smtp_credential_provider'),
          ]),
        ]);
        $config
          ->save();
        $messenger
          ->addStatus($this
          ->t('Drupal has been configured to send all e-mails using the SMTP transport type.'));
        break;
      case SWIFTMAILER_TRANSPORT_SENDMAIL:
        $config
          ->set('sendmail_path', $form_state
          ->getValue([
          'transport',
          'configuration',
          SWIFTMAILER_TRANSPORT_SENDMAIL,
          'path',
        ]));
        $config
          ->set('sendmail_mode', $form_state
          ->getValue([
          'transport',
          'configuration',
          SWIFTMAILER_TRANSPORT_SENDMAIL,
          'mode',
        ]));
        $config
          ->save();
        $messenger
          ->addStatus($this
          ->t('Drupal has been configured to send all e-mails using the Sendmail transport type.'));
        break;
      case SWIFTMAILER_TRANSPORT_NATIVE:
        $config
          ->save();
        $messenger
          ->addStatus($this
          ->t('Drupal has been configured to send all e-mails using the PHP transport type.'));
        break;
      case SWIFTMAILER_TRANSPORT_SPOOL:
        $config
          ->set('spool_directory', $form_state
          ->getValue([
          'transport',
          'configuration',
          SWIFTMAILER_TRANSPORT_SPOOL,
          'directory',
        ]));
        $config
          ->save();
        $messenger
          ->addStatus($this
          ->t('Drupal has been configured to send all e-mails using the Spool transport type.'));
        break;
    }
  }
}