You are here

SettingsForm.php in Drupalmonitor 8

File

src/Form/SettingsForm.php
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.
 */
class SettingsForm extends ConfigFormBase {

  /**
   * {@inheritdoc}
   */
  protected function getEditableConfigNames() {
    return [
      Settings::CONFIG_NAME,
    ];
  }

  /**
   * Drupal Monitor Settings.
   *
   * @var \Drupal\drupalmonitor\Settings
   */
  protected $drupalMonitorSettings;

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    $instance = parent::create($container);
    $instance->drupalMonitorSettings = $container
      ->get('drupalmonitor.settings');
    return $instance;
  }

  /**
   * {@inheritdoc}
   */
  public function getFormId() {
    return 'drupalmonitor_settings_form';
  }

  /**
   * Get Config.
   *
   * @return \Drupal\Core\Config\Config|\Drupal\Core\Config\ImmutableConfig
   *   Config.
   */
  protected function getConfig() {
    return $this
      ->config(Settings::CONFIG_NAME);
  }

  /**
   * {@inheritdoc}
   */
  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;
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    parent::submitForm($form, $form_state);

    // Get Config.
    $config = $this
      ->getConfig();

    // Set Hash.
    $config
      ->set(Settings::CONFIG_KEY_HASH, $form_state
      ->getValue(Settings::CONFIG_KEY_HASH));

    // Set Metrics.
    $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();
  }

}

Classes

Namesort descending Description
SettingsForm Class SettingsForm.