You are here

class IgnoredModulesForm in Nagios Monitoring 8

Hierarchy

Expanded class hierarchy of IgnoredModulesForm

1 string reference to 'IgnoredModulesForm'
nagios.routing.yml in ./nagios.routing.yml
nagios.routing.yml

File

src/Form/IgnoredModulesForm.php, line 13

Namespace

Drupal\nagios\Form
View source
class IgnoredModulesForm extends ConfigFormBase {

  /**
   * The module handler service.
   *
   * @var \Drupal\Core\Extension\ModuleHandlerInterface
   */
  protected $moduleHandler;

  /**
   * Constructs a ModulesListForm object.
   *
   * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
   *   The module handler.
   * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
   *   The factory for configuration objects.
   */
  public function __construct(ModuleHandlerInterface $module_handler, ConfigFactoryInterface $config_factory) {
    $this->moduleHandler = $module_handler;
    parent::__construct($config_factory);
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {

    /** @noinspection PhpParamsInspection */
    return new static($container
      ->get('module_handler'), $container
      ->get('config.factory'));
  }

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

  /**
   * {@inheritdoc}
   */
  protected function getEditableConfigNames() {
    return [
      'nagios.settings',
    ];
  }

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state) {
    $config = $this
      ->config('nagios.settings');
    $enabled = TRUE;
    $settings_url = Url::fromRoute('nagios.settings')
      ->toString();

    // Is the nagios module itself disabled?
    $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;
    }

    // Is "Checking of hook_requirements." disabled?
    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;
  }

  /**
   * Build the list of modules
   *
   * @param array $form
   *   An associative array containing the structure of the form.
   */
  protected function addDescription(array &$form) {
    $form['intro'] = [
      '#markup' => $this
        ->t('Select those modules that should be ignored for requirement checks.'),
    ];
  }

  /**
   * Build the list of modules
   *
   * @param array $form
   *   An associative array containing the structure of the form.
   * @param boolean $enabled
   *   Enable the check boxes
   */
  protected function buildTable(array &$form, $enabled) {
    $config = $this
      ->config('nagios.settings');
    $header = [
      'title' => $this
        ->t('Title'),
      'description' => $this
        ->t('Description'),
    ];
    $options = [];

    // Include system.admin.inc so we can use the sort callbacks.
    $this->moduleHandler
      ->loadInclude('system', 'inc', 'system.admin');

    // Sort all modules by their names.
    $modules = \Drupal::service('extension.list.module')
      ->getList();
    uasort($modules, 'system_sort_modules_by_info_name');

    // Build the rows
    foreach ($modules as $filename => $module) {
      if (empty($module->info['hidden'])) {
        $options[$filename] = $this
          ->buildRow($module);
        $options[$filename]['#disabled'] = TRUE;
      }
    }

    // Set up the check boxes
    $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;
      }
    }
  }

  /**
   * Build one row in the list of modules
   *
   * @param Extension $module
   *  The module that the row is build for
   *
   * @return array
   *  The row data for the table select element
   */
  protected function buildRow(Extension $module) {
    $row = [];
    $row['title'] = $module->info['name'];
    $row['description'] = $this
      ->t($module->info['description']);
    return $row;
  }

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

}

Members

Namesort descending Modifiers Type Description Overrides
ConfigFormBaseTrait::config protected function Retrieves a configuration object.
DependencySerializationTrait::$_entityStorages protected property An array of entity type IDs keyed by the property name of their storages.
DependencySerializationTrait::$_serviceIds protected property An array of service IDs keyed by property name used for serialization.
DependencySerializationTrait::__sleep public function 1
DependencySerializationTrait::__wakeup public function 2
FormBase::$configFactory protected property The config factory. 1
FormBase::$requestStack protected property The request stack. 1
FormBase::$routeMatch protected property The route match.
FormBase::configFactory protected function Gets the config factory for this form. 1
FormBase::container private function Returns the service container.
FormBase::currentUser protected function Gets the current user.
FormBase::getRequest protected function Gets the request object.
FormBase::getRouteMatch protected function Gets the route match.
FormBase::logger protected function Gets the logger for a specific channel.
FormBase::redirect protected function Returns a redirect response object for the specified route. Overrides UrlGeneratorTrait::redirect
FormBase::resetConfigFactory public function Resets the configuration factory.
FormBase::setConfigFactory public function Sets the config factory for this form.
FormBase::setRequestStack public function Sets the request stack object to use.
FormBase::validateForm public function Form validation handler. Overrides FormInterface::validateForm 62
IgnoredModulesForm::$moduleHandler protected property The module handler service.
IgnoredModulesForm::addDescription protected function Build the list of modules
IgnoredModulesForm::buildForm public function Form constructor. Overrides ConfigFormBase::buildForm
IgnoredModulesForm::buildRow protected function Build one row in the list of modules
IgnoredModulesForm::buildTable protected function Build the list of modules
IgnoredModulesForm::create public static function Instantiates a new instance of this class. Overrides ConfigFormBase::create
IgnoredModulesForm::getEditableConfigNames protected function Gets the configuration names that will be editable. Overrides ConfigFormBaseTrait::getEditableConfigNames
IgnoredModulesForm::getFormId public function Returns a unique string identifying the form. Overrides FormInterface::getFormId
IgnoredModulesForm::submitForm public function Form submission handler. Overrides ConfigFormBase::submitForm
IgnoredModulesForm::__construct public function Constructs a ModulesListForm object. Overrides ConfigFormBase::__construct
LinkGeneratorTrait::$linkGenerator protected property The link generator. 1
LinkGeneratorTrait::getLinkGenerator Deprecated protected function Returns the link generator.
LinkGeneratorTrait::l Deprecated protected function Renders a link to a route given a route name and its parameters.
LinkGeneratorTrait::setLinkGenerator Deprecated public function Sets the link generator service.
LoggerChannelTrait::$loggerFactory protected property The logger channel factory service.
LoggerChannelTrait::getLogger protected function Gets the logger for a specific channel.
LoggerChannelTrait::setLoggerFactory public function Injects the logger channel factory.
MessengerTrait::$messenger protected property The messenger. 29
MessengerTrait::messenger public function Gets the messenger. 29
MessengerTrait::setMessenger public function Sets the messenger.
RedirectDestinationTrait::$redirectDestination protected property The redirect destination service. 1
RedirectDestinationTrait::getDestinationArray protected function Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url.
RedirectDestinationTrait::getRedirectDestination protected function Returns the redirect destination service.
RedirectDestinationTrait::setRedirectDestination public function Sets the redirect destination service.
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.
UrlGeneratorTrait::$urlGenerator protected property The url generator.
UrlGeneratorTrait::getUrlGenerator Deprecated protected function Returns the URL generator service.
UrlGeneratorTrait::setUrlGenerator Deprecated public function Sets the URL generator service.
UrlGeneratorTrait::url Deprecated protected function Generates a URL or path for a specific route based on the given parameters.