Repository URL to install this package:
|
Version:
1.1.0 ▾
|
<?php
namespace Drupal\dds_sendgrid\Form;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;
use SendGrid\Mail\Content;
use SendGrid\Mail\From;
use SendGrid\Mail\Mail;
use SendGrid\Mail\Personalization;
use SendGrid\Mail\Subject;
use SendGrid\Mail\Substitution;
use SendGrid\Mail\To;
use Symfony\Component\DependencyInjection\ContainerInterface;
class SettingsForm extends ConfigFormBase {
/**
* The module handler service.
*
* @var \Drupal\Core\Extension\ModuleHandlerInterface
*/
protected $moduleHandler;
/**
* Constructs a \Drupal\system\ConfigFormBase object.
*
* @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
* The factory for configuration objects.
* @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
* The module handler service.
*/
public function __construct(ConfigFactoryInterface $config_factory, ModuleHandlerInterface $module_handler) {
parent::__construct($config_factory);
$this->moduleHandler = $module_handler;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static(
$container->get('config.factory'),
$container->get('module_handler')
);
}
/**
* {@inheritdoc}
*/
protected function getEditableConfigNames() {
return ['dds_sendgrid.settings'];
}
/**
* {@inheritdoc}
*/
public function getFormId() {
return 'sendgrid_settings_form';
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state) {
$config = $this->config('dds_sendgrid.settings');
$form['authentication'] = [
'#type' => 'fieldset',
'#title' => $this->t('Authentication'),
];
$form['authentication']['api_key'] = [
'#type' => 'password',
'#required' => TRUE,
'#title' => $this->t('API Secret Key'),
'#description' => $this->t('The secret key of your key pair. These are only generated once by SendGrid. Your existing key is hidden. If you need to change this, provide a new key here.'),
];
$api_key = $config->get('api_key');
if (!empty($api_key)) {
$form['authentication']['api_key']['#required'] = FALSE;
$form['authentication']['api_key']['#default_value'] = $api_key;
$form['authentication']['secretkeynotice'] = [
'#markup' => $this->t('You have saved a secret key. You may change the key by inputting a new one in the field directly below.'),
];
}
return parent::buildForm($form, $form_state);
}
/**
* {@inheritdoc}
*/
public function validateForm(array &$form, FormStateInterface $form_state) {
$config = $this->config('dds_sendgrid.settings');
// Check for API secret key. If missing throw error.
if (empty($config->get('api_key')) && empty($form_state->getValue('api_key'))) {
$form_state->setError($form['authentication']['api_key'], $this->t('You have not stored an API Secret Key.'));
}
parent::validateForm($form, $form_state);
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
$config = $this->config('dds_sendgrid.settings');
$values = $form_state->cleanValues()->getValues();
if (isset($values['api_key']) && !empty($values['api_key'])) {
$config->set('api_key', $values['api_key']);
}
$config->save();
parent::submitForm($form, $form_state);
}
}