View source
<?php
namespace Drupal\nagios\Form;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Extension\Extension;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
use Symfony\Component\DependencyInjection\ContainerInterface;
class IgnoredModulesForm extends ConfigFormBase {
protected $moduleHandler;
public function __construct(ModuleHandlerInterface $module_handler, ConfigFactoryInterface $config_factory) {
$this->moduleHandler = $module_handler;
parent::__construct($config_factory);
}
public static function create(ContainerInterface $container) {
return new static($container
->get('module_handler'), $container
->get('config.factory'));
}
public function getFormId() {
return 'nagios_ignored_modules';
}
protected function getEditableConfigNames() {
return [
'nagios.settings',
];
}
public function buildForm(array $form, FormStateInterface $form_state) {
$config = $this
->config('nagios.settings');
$enabled = TRUE;
$settings_url = Url::fromRoute('nagios.settings')
->toString();
$nagios_hooks_enabled = $config
->get('nagios.enable.nagios') ?? TRUE;
if (!$nagios_hooks_enabled) {
\Drupal::messenger()
->addMessage($this
->t('These settings are not available, because the nagios module is not enabled within the <a href="@nagios-settings">nagios settings</a>.', [
'@nagios-settings' => $settings_url,
]), 'warning');
$enabled = FALSE;
}
if (!$config
->get('nagios.function.requirements')) {
\Drupal::messenger()
->addMessage($this
->t('These settings are not available, because the requirements check is not enabled within the <a href="@nagios-settings">nagios settings</a>.', [
'@nagios-settings' => $settings_url,
]), 'warning');
$enabled = FALSE;
}
$this
->addDescription($form);
$this
->buildTable($form, $enabled);
$form = parent::buildForm($form, $form_state);
if (!$enabled) {
$form['actions']['submit']['#disabled'] = TRUE;
}
return $form;
}
protected function addDescription(array &$form) {
$form['intro'] = [
'#markup' => $this
->t('Select those modules that should be ignored for requirement checks.'),
];
}
protected function buildTable(array &$form, $enabled) {
$config = $this
->config('nagios.settings');
$header = [
'title' => $this
->t('Title'),
'description' => $this
->t('Description'),
];
$options = [];
$this->moduleHandler
->loadInclude('system', 'inc', 'system.admin');
$modules = \Drupal::service('extension.list.module')
->getList();
uasort($modules, 'system_sort_modules_by_info_name');
foreach ($modules as $filename => $module) {
if (empty($module->info['hidden'])) {
$options[$filename] = $this
->buildRow($module);
$options[$filename]['#disabled'] = TRUE;
}
}
$defaults = [];
$nagios_ignored_modules = $config
->get('nagios.ignored_modules') ?: [];
foreach ($nagios_ignored_modules as $ignored_module) {
$defaults[$ignored_module] = 1;
}
$form['modules'] = [
'#type' => 'tableselect',
'#header' => $header,
'#options' => $options,
'#empty' => $this
->t('No modules available.'),
'#default_value' => $defaults,
];
if (!$enabled) {
foreach ($form['modules']['#options'] as $key => $value) {
$form['modules']['#options']['#disabled'] = TRUE;
}
}
}
protected function buildRow(Extension $module) {
$row = [];
$row['title'] = $module->info['name'];
$row['description'] = $this
->t($module->info['description']);
return $row;
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$config = $this
->config('nagios.settings');
$ignored_modules = array_keys(array_filter($form_state
->getValue('modules')));
$config
->set('nagios.ignored_modules', $ignored_modules);
$config
->save();
parent::submitForm($form, $form_state);
}
}