View source
<?php
namespace Drupal\drupalmonitor\Form;
use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
use Drupal\drupalmonitor\Settings;
use Symfony\Component\DependencyInjection\ContainerInterface;
class SettingsForm extends ConfigFormBase {
protected function getEditableConfigNames() {
return [
Settings::CONFIG_NAME,
];
}
protected $drupalMonitorSettings;
public static function create(ContainerInterface $container) {
$instance = parent::create($container);
$instance->drupalMonitorSettings = $container
->get('drupalmonitor.settings');
return $instance;
}
public function getFormId() {
return 'drupalmonitor_settings_form';
}
protected function getConfig() {
return $this
->config(Settings::CONFIG_NAME);
}
public function buildForm(array $form, FormStateInterface $form_state) {
$form['drupalmonitor_hash'] = [
'#type' => 'textfield',
'#title' => t('Security Hash'),
'#default_value' => $this->drupalMonitorSettings
->getHash(),
'#required' => TRUE,
'#description' => $this
->t('Security hash which will be used to protect the access @link.', [
'@link' => Url::fromRoute('drupalmonitor.drupalmonitor', [], [
'absolute' => TRUE,
])
->toString(),
]),
];
$form['metrics'] = [
'#type' => 'fieldset',
'#title' => $this
->t('Metrics'),
'#description' => $this
->t('Enable all the metric Drupalmonitor should list.'),
];
$form['metrics'][Settings::CONFIG_KEY_SERVER_MONITORING] = [
'#type' => 'checkbox',
'#title' => $this
->t('Server'),
'#default_value' => $this->drupalMonitorSettings
->isServerMonitoringEnabled(),
];
$form['metrics'][Settings::CONFIG_KEY_USER_MONITORING] = [
'#type' => 'checkbox',
'#title' => $this
->t('User'),
'#default_value' => $this->drupalMonitorSettings
->isUserMonitoringEnabled(),
];
$form['metrics'][Settings::CONFIG_KEY_FILES_MONITORING] = [
'#type' => 'checkbox',
'#title' => $this
->t('Files'),
'#default_value' => $this->drupalMonitorSettings
->isFilesMonitoringEnabled(),
];
$form['metrics'][Settings::CONFIG_KEY_NODE_MONITORING] = [
'#type' => 'checkbox',
'#title' => $this
->t('Nodes'),
'#default_value' => $this->drupalMonitorSettings
->isNodeMonitoringEnabled(),
];
$form['metrics'][Settings::CONFIG_KEY_MODULES_MONITORING] = [
'#type' => 'checkbox',
'#title' => $this
->t('Modules'),
'#default_value' => $this->drupalMonitorSettings
->isModulesMonitoringEnabled(),
];
$form['metrics'][Settings::CONFIG_KEY_VARIABLES_MONITORING] = [
'#type' => 'checkbox',
'#title' => $this
->t('Variables / Configurations'),
'#default_value' => $this->drupalMonitorSettings
->isVariablesMonitoringEnabled(),
];
$form['submit'] = [
'#type' => 'submit',
'#value' => $this
->t('Submit'),
];
return $form;
}
public function submitForm(array &$form, FormStateInterface $form_state) {
parent::submitForm($form, $form_state);
$config = $this
->getConfig();
$config
->set(Settings::CONFIG_KEY_HASH, $form_state
->getValue(Settings::CONFIG_KEY_HASH));
$metrics_to_save = [
Settings::CONFIG_KEY_SERVER_MONITORING,
Settings::CONFIG_KEY_USER_MONITORING,
Settings::CONFIG_KEY_FILES_MONITORING,
Settings::CONFIG_KEY_NODE_MONITORING,
Settings::CONFIG_KEY_MODULES_MONITORING,
Settings::CONFIG_KEY_VARIABLES_MONITORING,
];
foreach ($metrics_to_save as $metric_key) {
$config
->set($metric_key, (int) $form_state
->getValue($metric_key));
}
$config
->save();
}
}