You are here

public function EnabledModulesSensorPlugin::buildConfigurationForm in Monitoring 8

Form constructor.

Plugin forms are embedded in other forms. In order to know where the plugin form is located in the parent form, #parents and #array_parents must be known, but these are not available during the initial build phase. In order to have these properties available when building the plugin form's elements, let this method return a form element that has a #process callback and build the rest of the form in the callback. By the time the callback is executed, the element's #parents and #array_parents properties will have been set by the form API. For more documentation on #parents and #array_parents, see \Drupal\Core\Render\Element\FormElement.

Parameters

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

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form. Calling code should pass on a subform state created through \Drupal\Core\Form\SubformState::createForSubform().

Return value

array The form structure.

Overrides SensorPluginBase::buildConfigurationForm

File

src/Plugin/monitoring/SensorPlugin/EnabledModulesSensorPlugin.php, line 31
Contains \Drupal\monitoring\Plugin\monitoring\SensorPlugin\EnabledModulesSensorPlugin.

Class

EnabledModulesSensorPlugin
Monitors installed modules.

Namespace

Drupal\monitoring\Plugin\monitoring\SensorPlugin

Code

public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
  $form = parent::buildConfigurationForm($form, $form_state);

  // Run the sensor to current setting and display the update button if
  // sensor result is critical.
  $configured_modules = array_filter($this->sensorConfig
    ->getSetting('modules', NULL));

  // If the sensor is not configured, select installed modules.
  if (!$configured_modules) {
    $enabled_modules = Drupal::moduleHandler()
      ->getModuleList();

    // Reduce to the module name only.
    $configured_modules = array_combine(array_keys($enabled_modules), array_keys($enabled_modules));
  }
  else {

    // Run on a temporary sensor config id with some changes.

    /** @var \Drupal\monitoring\Entity\SensorConfig $run_config */
    $run_config = $this->sensorConfig
      ->createDuplicate();

    // Avoid name clashes in SensorManager / caching.
    $run_config->id = $this->sensorConfig
      ->id() . '_temp';

    // Force enabling the sensor for running.
    $run_config->status = TRUE;

    // Force no additional allowed to make differences visible in message.
    $run_config->settings['allow_additional'] = FALSE;

    /** @var \Drupal\monitoring\Result\SensorResult $result */
    $result = \Drupal::service('monitoring.sensor_runner')
      ->runSensors(array(
      $run_config
        ->id() => $run_config,
    ), TRUE)[$run_config
      ->id()];
    if ($result
      ->isCritical()) {
      $message = $result
        ->getMessage();

      // Display message and button to update selection.
      $form['update_modules']['message'] = array(
        '#type' => 'item',
        '#title' => t('Test run message'),
        '#markup' => $message,
      );
      $form['update_modules']['update'] = array(
        '#type' => 'submit',
        '#value' => t('Update module selection'),
        '#limit_validation_errors' => array(),
        '#submit' => array(
          array(
            $this,
            'updateModuleListSubmit',
          ),
        ),
        '#ajax' => array(
          'callback' => '::ajaxReplacePluginSpecificForm',
          'wrapper' => 'monitoring-sensor-plugin',
          'method' => 'replace',
        ),
      );
    }
  }
  $form['allow_additional'] = array(
    '#type' => 'checkbox',
    '#title' => t('Allow additional modules to be installed'),
    '#description' => t('If checked additionally installed modules will not be considered a critical state.'),
    '#default_value' => $this->sensorConfig
      ->getSetting('allow_additional'),
  );

  // Get current list of available modules.
  // @todo find a faster solution? If that happens we can drop caching the
  //   result for 1 hour.
  $modules = \Drupal::service('extension.list.module')
    ->getList();
  uasort($modules, 'system_sort_modules_by_info_name');
  $visible_modules = array();
  $visible_default_value = array();
  $hidden_modules = array();
  $hidden_default_value = array();
  foreach ($modules as $module => $module_data) {

    // Skip profiles.
    if (strpos(drupal_get_path('module', $module), 'profiles') === 0) {
      continue;
    }

    // As we also include hidden modules, some might have no name at all,
    // make sure it is set.
    if (!isset($module_data->info['name'])) {
      $module_data->info['name'] = '- No name -';
    }
    if (!empty($module_data->info['hidden'])) {
      $hidden_modules[$module] = $module_data->info['name'] . ' (' . $module . ')';
      if (!empty($configured_modules[$module])) {
        $hidden_default_value[$module] = $configured_modules[$module];
      }
    }
    else {
      $visible_modules[$module] = $module_data->info['name'] . ' (' . $module . ')';
      if (!empty($configured_modules[$module])) {
        $visible_default_value[$module] = $configured_modules[$module];
      }
    }
  }
  $form['modules'] = array(
    '#type' => 'checkboxes',
    '#options' => $visible_modules,
    '#title' => t('Modules expected to be installed'),
    '#description' => t('Check all modules that are supposed to be installed.'),
    '#default_value' => $visible_default_value,
  );
  $form['extended'] = array(
    '#type' => 'details',
    '#title' => 'Extended',
    '#open' => count($hidden_default_value) ? TRUE : FALSE,
  );
  $form['extended']['modules_hidden'] = array(
    '#type' => 'checkboxes',
    '#options' => $hidden_modules,
    '#title' => t('Hidden modules expected to be installed'),
    '#default_value' => $hidden_default_value,
    '#description' => t('Check all modules that are supposed to be installed.'),
  );
  return $form;
}