class SettingsForm in Sparkpost email 8.2
Same name in this branch
- 8.2 src/Form/SettingsForm.php \Drupal\sparkpost\Form\SettingsForm
- 8.2 modules/sparkpost_requeue/src/Form/SettingsForm.php \Drupal\sparkpost_requeue\Form\SettingsForm
Same name and namespace in other branches
- 8 src/Form/SettingsForm.php \Drupal\sparkpost\Form\SettingsForm
Settings form for the Sparkpost module.
@package Drupal\sparkpost\Form
Hierarchy
- class \Drupal\Core\Form\FormBase implements ContainerInjectionInterface, FormInterface uses DependencySerializationTrait, LoggerChannelTrait, MessengerTrait, LinkGeneratorTrait, RedirectDestinationTrait, UrlGeneratorTrait, StringTranslationTrait
- class \Drupal\Core\Form\ConfigFormBase uses ConfigFormBaseTrait
- class \Drupal\sparkpost\Form\SettingsForm
- class \Drupal\Core\Form\ConfigFormBase uses ConfigFormBaseTrait
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\FormView 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
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
ConfigFormBase:: |
public static | function |
Instantiates a new instance of this class. Overrides FormBase:: |
13 |
ConfigFormBase:: |
public | function | Constructs a \Drupal\system\ConfigFormBase object. | 11 |
ConfigFormBaseTrait:: |
protected | function | Retrieves a configuration object. | |
DependencySerializationTrait:: |
protected | property | An array of entity type IDs keyed by the property name of their storages. | |
DependencySerializationTrait:: |
protected | property | An array of service IDs keyed by property name used for serialization. | |
DependencySerializationTrait:: |
public | function | 1 | |
DependencySerializationTrait:: |
public | function | 2 | |
FormBase:: |
protected | property | The config factory. | 1 |
FormBase:: |
protected | property | The request stack. | 1 |
FormBase:: |
protected | property | The route match. | |
FormBase:: |
protected | function | Gets the config factory for this form. | 1 |
FormBase:: |
private | function | Returns the service container. | |
FormBase:: |
protected | function | Gets the current user. | |
FormBase:: |
protected | function | Gets the request object. | |
FormBase:: |
protected | function | Gets the route match. | |
FormBase:: |
protected | function | Gets the logger for a specific channel. | |
FormBase:: |
protected | function |
Returns a redirect response object for the specified route. Overrides UrlGeneratorTrait:: |
|
FormBase:: |
public | function | Resets the configuration factory. | |
FormBase:: |
public | function | Sets the config factory for this form. | |
FormBase:: |
public | function | Sets the request stack object to use. | |
FormBase:: |
public | function |
Form validation handler. Overrides FormInterface:: |
62 |
LinkGeneratorTrait:: |
protected | property | The link generator. | 1 |
LinkGeneratorTrait:: |
protected | function | Returns the link generator. | |
LinkGeneratorTrait:: |
protected | function | Renders a link to a route given a route name and its parameters. | |
LinkGeneratorTrait:: |
public | function | Sets the link generator service. | |
LoggerChannelTrait:: |
protected | property | The logger channel factory service. | |
LoggerChannelTrait:: |
protected | function | Gets the logger for a specific channel. | |
LoggerChannelTrait:: |
public | function | Injects the logger channel factory. | |
MessengerTrait:: |
protected | property | The messenger. | 29 |
MessengerTrait:: |
public | function | Gets the messenger. | 29 |
MessengerTrait:: |
public | function | Sets the messenger. | |
RedirectDestinationTrait:: |
protected | property | The redirect destination service. | 1 |
RedirectDestinationTrait:: |
protected | function | Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url. | |
RedirectDestinationTrait:: |
protected | function | Returns the redirect destination service. | |
RedirectDestinationTrait:: |
public | function | Sets the redirect destination service. | |
SettingsForm:: |
public | function |
Form constructor. Overrides ConfigFormBase:: |
|
SettingsForm:: |
protected | function |
Gets the configuration names that will be editable. Overrides ConfigFormBaseTrait:: |
|
SettingsForm:: |
public | function |
Returns a unique string identifying the form. Overrides FormInterface:: |
|
SettingsForm:: |
constant | The plugin id for our mailer. | ||
SettingsForm:: |
public | function |
Form submission handler. Overrides ConfigFormBase:: |
|
StringTranslationTrait:: |
protected | property | The string translation service. | 1 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. | |
UrlGeneratorTrait:: |
protected | property | The url generator. | |
UrlGeneratorTrait:: |
protected | function | Returns the URL generator service. | |
UrlGeneratorTrait:: |
public | function | Sets the URL generator service. | |
UrlGeneratorTrait:: |
protected | function | Generates a URL or path for a specific route based on the given parameters. |