You are here

public function SettingsForm::submitForm in Nagios Monitoring 8

Form submission handler.

Parameters

array $form: An associative array containing the structure of the form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

Overrides ConfigFormBase::submitForm

File

src/Form/SettingsForm.php, line 299

Class

SettingsForm
Allows admins to configure the Nagios module.

Namespace

Drupal\nagios\Form

Code

public function submitForm(array &$form, FormStateInterface $form_state) {
  $config = $this
    ->config('nagios.settings');
  $config
    ->set('nagios.ua', $form_state
    ->getValue('nagios_ua'));
  $config
    ->set('nagios.show_outdated_names', $form_state
    ->getValue('nagios_show_outdated_names'));
  $status_page = $form_state
    ->getValue('nagios_enable_status_page');
  $config
    ->set('nagios.statuspage.enabled', (bool) $status_page);
  if ($status_page) {
    $route_info_changed = $config
      ->get('nagios.statuspage.path') != $form_state
      ->getValue('nagios_page_path') || $config
      ->get('nagios.statuspage.controller') != $form_state
      ->getValue('nagios_page_controller');
  }
  $config
    ->set('nagios.statuspage.path', $form_state
    ->getValue('nagios_page_path'));
  $config
    ->set('nagios.statuspage.controller', $form_state
    ->getValue('nagios_page_controller'));
  $config
    ->set('nagios.statuspage.getparam', $form_state
    ->getValue('nagios_enable_status_page_get'));
  $config
    ->set('nagios.status.ok', (int) $form_state
    ->getValue('nagios_status_ok_value'));
  $config
    ->set('nagios.status.warning', (int) $form_state
    ->getValue('nagios_status_warning_value'));
  $config
    ->set('nagios.status.critical', (int) $form_state
    ->getValue('nagios_status_critical_value'));
  $config
    ->set('nagios.status.unknown', (int) $form_state
    ->getValue('nagios_status_unknown_value'));
  foreach (nagios_invoke_all('nagios_info') as $module => $data) {
    $config
      ->set('nagios.enable.' . $module, $form_state
      ->getValue('nagios_enable_' . $module));
  }
  $config
    ->set('nagios.limit_watchdog.display', $form_state
    ->getValue('limit_watchdog_display'));
  $config
    ->set('nagios.limit_watchdog.channel_filter', $form_state
    ->getValue('channel_filter'));
  $config
    ->set('nagios.limit_watchdog.negate', $form_state
    ->getValue('negate'));
  foreach (nagios_invoke_all('nagios_settings') as $module => $module_settings) {
    foreach ($module_settings as $element => $data) {

      // Save config for first level form elements.
      // For the nagios module, this saves if check functions are enabled,
      // e.g. "nagios.function.cron".
      if (isset($data['#configname'])) {
        $config
          ->set($module . '.' . $data['#configname'], $form_state
          ->getValue($element, $config
          ->get($module . '.' . $data['#configname'])));
      }

      // Save config for second level form elements:
      if (isset($data['#type']) && $data['#type'] == 'fieldset') {
        foreach ($data as $fieldset_element => $fieldset_data) {
          if (is_array($fieldset_data) && !isset($fieldset_data['#default_value']) && isset($fieldset_data['#configname'])) {
            $config
              ->set($module . '.' . $fieldset_data['#configname'], $form_state
              ->getValue($fieldset_element, $config
              ->get($module . '.' . $fieldset_data['#configname'])));
          }
        }
      }
    }
  }
  $config
    ->save();
  if (!empty($route_info_changed)) {
    \Drupal::service('router.builder')
      ->rebuild();
    \Drupal::cache('menu')
      ->invalidateAll();
  }
  parent::submitForm($form, $form_state);
}