View source
<?php
namespace Drupal\logs_http\Form;
use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Logger\RfcLogLevel;
use Symfony\Component\HttpFoundation\Request;
class SettingsForm extends ConfigFormBase {
public function getFormId() {
return 'logs_http_admin_settings_form';
}
protected function getEditableConfigNames() {
return [
'logs_http.settings',
];
}
public function defaultKeys() {
return [
'enabled',
'url',
'severity_level',
'uuid',
];
}
public function buildForm(array $form, FormStateInterface $form_state, Request $request = NULL) {
$config = $this
->config('logs_http.settings');
$form['enabled'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Enable'),
'#description' => $this
->t('Enable Logs HTTP'),
'#default_value' => $config
->get('enabled'),
];
$form['url'] = [
'#type' => 'url',
'#title' => $this
->t('Endpoint'),
'#description' => $this
->t('The URL to POST the data to.'),
'#default_value' => $config
->get('url'),
];
$form['severity_level'] = [
'#type' => 'select',
'#title' => $this
->t('Watchdog Severity'),
'#options' => RfcLogLevel::getLevels(),
'#default_value' => $config
->get('severity_level'),
'#description' => $this
->t('The minimum severity level to be reached before an event is sent to Logs.'),
];
$form['environment_uuid'] = [
'#type' => 'textfield',
'#title' => $this
->t('Unique ID'),
'#description' => $this
->t('An arbitrary ID that will identify the environment.'),
'#default_value' => $config
->get('environment_uuid'),
];
return parent::buildForm($form, $form_state);
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$config = $this
->config('logs_http.settings');
foreach ($this
->defaultKeys() as $key) {
$config
->set($key, $form_state
->getValue($key));
}
$config
->save();
parent::submitForm($form, $form_state);
}
}