You are here

class SettingsForm in Sparkpost email 8.2

Same name in this branch
  1. 8.2 src/Form/SettingsForm.php \Drupal\sparkpost\Form\SettingsForm
  2. 8.2 modules/sparkpost_requeue/src/Form/SettingsForm.php \Drupal\sparkpost_requeue\Form\SettingsForm
Same name and namespace in other branches
  1. 8 src/Form/SettingsForm.php \Drupal\sparkpost\Form\SettingsForm

Settings form for the Sparkpost module.

@package Drupal\sparkpost\Form

Hierarchy

Expanded class hierarchy of SettingsForm

2 string references to 'SettingsForm'
sparkpost.routing.yml in ./sparkpost.routing.yml
sparkpost.routing.yml
sparkpost_requeue.routing.yml in modules/sparkpost_requeue/sparkpost_requeue.routing.yml
modules/sparkpost_requeue/sparkpost_requeue.routing.yml

File

src/Form/SettingsForm.php, line 14

Namespace

Drupal\sparkpost\Form
View source
class SettingsForm extends ConfigFormBase {

  /**
   * The plugin id for our mailer.
   */
  const MAIL_KEY = 'sparkpost_mail';

  /**
   * {@inheritdoc}
   */
  protected function getEditableConfigNames() {
    return [
      'sparkpost.settings',
    ];
  }

  /**
   * {@inheritdoc}
   */
  public function getFormId() {
    return 'settings_form';
  }

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state) {
    $config = $this
      ->config('sparkpost.settings');
    $key = $config
      ->get('api_key');
    $form['api_key'] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('API Key'),
      '#default_value' => $key,
      '#description' => $this
        ->t('Create or grab your API key from the <a href="@url">Sparkpost settings.</a>', [
        '@url' => 'https://app.sparkpost.com/account/credentials',
      ]),
    ];
    if ($key) {

      // Check if we want to warn the user about the fact that Sparkpost is not
      // being used as mail system.
      if (\Drupal::moduleHandler()
        ->moduleExists('mailsystem')) {

        // Check config for mailsystem.
        $default_sender = $this
          ->config('mailsystem.settings')
          ->get('defaults.sender');
        if ($default_sender != $this::MAIL_KEY) {
          $this
            ->messenger()
            ->addWarning($this
            ->t('It seems you are using the mailsystem module to control your mail, but the default sender is not set to Sparkpost. If this is not on purpose you should probably <a href="@url">adjust the settings at this page.</a>', [
            '@url' => Url::fromRoute('mailsystem.settings')
              ->toString(),
          ]));
        }
      }
      else {

        // See if the config sets it to something else than sparkpost.
        $config_collection = 'system.mail';
        $config_key = 'interface.default';
        $default_system = $this
          ->config($config_collection)
          ->get($config_key);
        if ($default_system != $this::MAIL_KEY) {
          $this
            ->messenger()
            ->addWarning($this
            ->t('You seem to be using %system as your mail system instead of %sparkpost. If this is not on purpose, you should change the configration for %config_key in %config to %sparkpost.', [
            '%system' => $default_system,
            '%sparkpost' => $this::MAIL_KEY,
            '%config_key' => $config_key,
            '%config' => $config_collection,
          ]));
        }
      }
      $form['api_hostname'] = [
        '#title' => $this
          ->t('API Hostname'),
        '#type' => 'select',
        '#options' => [
          'api.sparkpost.com' => 'SparkPost (api.sparkpost.com)',
          'api.eu.sparkpost.com' => 'SparkPost EU (api.eu.sparkpost.com)',
        ],
        '#description' => $this
          ->t('SparkPost EU is the full SparkPost service hosted in Western Europe. An account created in SparkPost cannot be used in SparkPost EU, and vice-versa.'),
        '#default_value' => $config
          ->get('api_hostname'),
      ];
      $form['options'] = [
        '#type' => 'fieldset',
        '#title' => $this
          ->t('Email options'),
        '#collapsible' => TRUE,
      ];
      $form['options']['debug'] = [
        '#type' => 'checkbox',
        '#title' => $this
          ->t('Debug'),
        '#default_value' => $config
          ->get('debug') ? TRUE : FALSE,
        '#description' => $this
          ->t('If selected, exceptions will be sent over to watchdog.'),
      ];
      $form['options']['sender'] = [
        '#type' => 'email',
        '#title' => $this
          ->t('From address'),
        '#default_value' => $config
          ->get('sender'),
        '#description' => $this
          ->t('The sender email address. If this address has not been verified, messages will not be sent. This address will appear in the "from" field, and any emails sent through Sparkpost with a "from" address will have that address moved to the Reply-To field.'),
        '#required' => TRUE,
      ];
      $form['options']['sender_name'] = [
        '#type' => 'textfield',
        '#title' => $this
          ->t('From name'),
        '#default_value' => $config
          ->get('sender_name'),
        '#description' => $this
          ->t('Optionally enter a from name to be used.'),
      ];
      $options = [
        '' => $this
          ->t('-- Select --'),
      ];
      $formats = filter_formats();
      foreach ($formats as $key => $format) {
        $options[$key] = $format
          ->label();
      }
      $form['options']['format'] = [
        '#title' => $this
          ->t('Input format'),
        '#type' => 'select',
        '#options' => $options,
        '#description' => $this
          ->t('If selected, the input format to apply to the message body before sending to the Sparkpost API.'),
        '#default_value' => $config
          ->get('format'),
      ];
      $form['options']['async'] = [
        '#title' => $this
          ->t('Send asynchronous'),
        '#default_value' => $config
          ->get('async'),
        '#type' => 'checkbox',
        '#description' => $this
          ->t('If selected, the mails will not be sent right away, but queued and possibly sent with cron or drush.'),
      ];
      $form['options']['skip_cron'] = [
        '#type' => 'checkbox',
        '#title' => $this
          ->t('Skip queue on cron'),
        '#default_value' => $config
          ->get('skip_cron'),
        '#description' => $this
          ->t('If selected, the mail queue will not be processed by cron runs. Use this only if you manually run the queue with drush (for example).'),
        '#states' => [
          'visible' => [
            ':input[name="async"]' => [
              'checked' => TRUE,
            ],
          ],
        ],
      ];
    }
    return parent::buildForm($form, $form_state);
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    parent::submitForm($form, $form_state);
    $this
      ->config('sparkpost.settings')
      ->set('api_hostname', $form_state
      ->getValue('api_hostname'))
      ->set('api_key', $form_state
      ->getValue('api_key'))
      ->set('debug', $form_state
      ->getValue('debug'))
      ->set('sender_name', $form_state
      ->getValue('sender_name'))
      ->set('sender', $form_state
      ->getValue('sender'))
      ->set('format', $form_state
      ->getValue('format'))
      ->set('async', $form_state
      ->getValue('async'))
      ->set('skip_cron', $form_state
      ->getValue('skip_cron'))
      ->save();
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ConfigFormBase::create public static function Instantiates a new instance of this class. Overrides FormBase::create 13
ConfigFormBase::__construct public function Constructs a \Drupal\system\ConfigFormBase object. 11
ConfigFormBaseTrait::config protected function Retrieves a configuration object.
DependencySerializationTrait::$_entityStorages protected property An array of entity type IDs keyed by the property name of their storages.
DependencySerializationTrait::$_serviceIds protected property An array of service IDs keyed by property name used for serialization.
DependencySerializationTrait::__sleep public function 1
DependencySerializationTrait::__wakeup public function 2
FormBase::$configFactory protected property The config factory. 1
FormBase::$requestStack protected property The request stack. 1
FormBase::$routeMatch protected property The route match.
FormBase::configFactory protected function Gets the config factory for this form. 1
FormBase::container private function Returns the service container.
FormBase::currentUser protected function Gets the current user.
FormBase::getRequest protected function Gets the request object.
FormBase::getRouteMatch protected function Gets the route match.
FormBase::logger protected function Gets the logger for a specific channel.
FormBase::redirect protected function Returns a redirect response object for the specified route. Overrides UrlGeneratorTrait::redirect
FormBase::resetConfigFactory public function Resets the configuration factory.
FormBase::setConfigFactory public function Sets the config factory for this form.
FormBase::setRequestStack public function Sets the request stack object to use.
FormBase::validateForm public function Form validation handler. Overrides FormInterface::validateForm 62
LinkGeneratorTrait::$linkGenerator protected property The link generator. 1
LinkGeneratorTrait::getLinkGenerator Deprecated protected function Returns the link generator.
LinkGeneratorTrait::l Deprecated protected function Renders a link to a route given a route name and its parameters.
LinkGeneratorTrait::setLinkGenerator Deprecated public function Sets the link generator service.
LoggerChannelTrait::$loggerFactory protected property The logger channel factory service.
LoggerChannelTrait::getLogger protected function Gets the logger for a specific channel.
LoggerChannelTrait::setLoggerFactory public function Injects the logger channel factory.
MessengerTrait::$messenger protected property The messenger. 29
MessengerTrait::messenger public function Gets the messenger. 29
MessengerTrait::setMessenger public function Sets the messenger.
RedirectDestinationTrait::$redirectDestination protected property The redirect destination service. 1
RedirectDestinationTrait::getDestinationArray protected function Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url.
RedirectDestinationTrait::getRedirectDestination protected function Returns the redirect destination service.
RedirectDestinationTrait::setRedirectDestination public function Sets the redirect destination service.
SettingsForm::buildForm public function Form constructor. Overrides ConfigFormBase::buildForm
SettingsForm::getEditableConfigNames protected function Gets the configuration names that will be editable. Overrides ConfigFormBaseTrait::getEditableConfigNames
SettingsForm::getFormId public function Returns a unique string identifying the form. Overrides FormInterface::getFormId
SettingsForm::MAIL_KEY constant The plugin id for our mailer.
SettingsForm::submitForm public function Form submission handler. Overrides ConfigFormBase::submitForm
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.
UrlGeneratorTrait::$urlGenerator protected property The url generator.
UrlGeneratorTrait::getUrlGenerator Deprecated protected function Returns the URL generator service.
UrlGeneratorTrait::setUrlGenerator Deprecated public function Sets the URL generator service.
UrlGeneratorTrait::url Deprecated protected function Generates a URL or path for a specific route based on the given parameters.