externalauth_gitlab-8.x-1.x-dev/src/Form/SettingsForm.php
src/Form/SettingsForm.php
<?php
namespace Drupal\externalauth_gitlab\Form;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Form\ConfigFormBaseTrait;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* {@inheritdoc}
*/
class SettingsForm extends FormBase {
use ConfigFormBaseTrait;
/**
* Config settings.
*
* @var string
*/
const SETTINGS = 'externalauth_gitlab.settings';
/**
* The config factory object.
*
* @var \Drupal\Core\Config\ConfigFactoryInterface
*/
protected $configFactory;
/**
* Constructs a SettingsForm object.
*
* @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
* A configuration factory instance.
*/
public function __construct(ConfigFactoryInterface $config_factory) {
$this->configFactory = $config_factory;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static(
$container->get('config.factory')
);
}
/**
* {@inheritdoc}
*/
public function getFormId() {
return 'settings_form';
}
/**
* {@inheritdoc}
*/
protected function getEditableConfigNames() {
return [
static::SETTINGS,
];
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state) {
$config = $this->config(static::SETTINGS);
$form['client_id'] = [
'#type' => 'textfield',
'#title' => $this->t('Client ID'),
'#description' => $this->t('The client id'),
'#default_value' => $config->get('client_id'),
];
$form['client_secret'] = [
'#type' => 'textfield',
'#title' => $this->t('Client secret'),
'#description' => $this->t('The client secret'),
'#default_value' => $config->get('client_secret'),
];
$form['domain'] = [
'#type' => 'textfield',
'#title' => $this->t('Domain'),
'#default_value' => $config->get('domain'),
];
$form['submit'] = [
'#type' => 'submit',
'#value' => $this->t('Submit'),
];
return $form;
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
// Display result.
$config = $this->configFactory->getEditable('externalauth_gitlab.settings');
$keys = ['client_id', 'client_secret', 'domain'];
foreach ($keys as $key) {
$config->set($key, $form_state->getValue($key));
}
$config->save();
}
}
