You are here

public function DatabaseAggregatorSensorPlugin::validateConfigurationForm in Monitoring 8

Form validation handler.

Parameters

array $form: An associative array containing the structure of the plugin form as built by static::buildConfigurationForm().

\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().

Overrides SensorPluginBase::validateConfigurationForm

File

src/Plugin/monitoring/SensorPlugin/DatabaseAggregatorSensorPlugin.php, line 689
Contains \Drupal\monitoring\Plugin\monitoring\SensorPlugin\DatabaseAggregatorSensorPlugin.

Class

DatabaseAggregatorSensorPlugin
Database aggregator able to query a single db table.

Namespace

Drupal\monitoring\Plugin\monitoring\SensorPlugin

Code

public function validateConfigurationForm(array &$form, FormStateInterface $form_state) {
  parent::validateConfigurationForm($form, $form_state);

  /** @var \Drupal\Core\Database\Connection $database */
  $database = $this
    ->getService('database');
  $table = $form_state
    ->getValue(array(
    'settings',
    'table',
  ));
  $query = $database
    ->select($table);
  if (!$database
    ->schema()
    ->tableExists($table)) {
    try {
      $query
        ->range(0, 1)
        ->execute();
    } catch (\Exception $e) {
      $form_state
        ->setErrorByName('settings][table', t('The table %table does not exist in the database %database', [
        '%table' => $table,
        '%database' => $database
          ->getConnectionOptions()['database'],
      ]));
      return;
    }
  }
  $field_name = $form_state
    ->getValue(array(
    'settings',
    'aggregation',
    'time_interval_field',
  ));
  if (!empty($field_name)) {

    // @todo instead of validate, switch to a form select.
    if (!$database
      ->schema()
      ->fieldExists($table, $field_name)) {
      $form_state
        ->setErrorByName('settings][aggregation][time_interval_field', t('The specified time interval field %name does not exist in table %table.', array(
        '%name' => $field_name,
        '%table' => $table,
      )));
    }
  }

  // Validate verbose_fields.
  if ($this->configurableConditions) {
    $fields = $form_state
      ->getValue('verbose_fields', []);
    foreach ($fields as $key => $field) {
      $query = $database
        ->select($table);
      $field_name = $field['field_key'];
      if (!empty($field_name) && !$database
        ->schema()
        ->fieldExists($table, $field_name)) {
        $query
          ->addField($table, $field_name);
        try {
          $query
            ->range(0, 1)
            ->execute();
        } catch (\Exception $e) {
          $form_state
            ->setErrorByName("verbose_fields][{$key}][field_key", t('The field %field does not exist in the table "%table".', [
            '%field' => $field_name,
            '%table' => $table,
          ]));
          continue;
        }
      }
    }

    // Validate conditions.
    $fields = $form_state
      ->getValue('conditions', []);
    foreach ($fields as $key => $field) {
      $query = $database
        ->select($table);
      $field_name = $field['field'];
      if (!empty($field_name) && !$database
        ->schema()
        ->fieldExists($table, $field_name)) {
        $query
          ->addField($table, $field_name);
        try {
          $query
            ->range(0, 1)
            ->execute();
        } catch (\Exception $e) {
          $form_state
            ->setErrorByName("conditions][{$key}][field", t('The field %field does not exist in the table "%table".', [
            '%field' => $field_name,
            '%table' => $table,
          ]));
          continue;
        }
      }
    }
  }
}