diff --git a/modules/test/config/optional/monitoring.sensor_config.entity_aggregate_test.yml b/modules/test/config/optional/monitoring.sensor_config.entity_aggregate_test.yml index 3c9019f..29d8ef5 100644 --- a/modules/test/config/optional/monitoring.sensor_config.entity_aggregate_test.yml +++ b/modules/test/config/optional/monitoring.sensor_config.entity_aggregate_test.yml @@ -15,7 +15,7 @@ settings: - field: 'type' value: 'page' - fields: + verbose_fields: - id - label thresholds: diff --git a/src/Plugin/monitoring/SensorPlugin/DatabaseAggregatorSensorPlugin.php b/src/Plugin/monitoring/SensorPlugin/DatabaseAggregatorSensorPlugin.php index 6995507..c074f80 100644 --- a/src/Plugin/monitoring/SensorPlugin/DatabaseAggregatorSensorPlugin.php +++ b/src/Plugin/monitoring/SensorPlugin/DatabaseAggregatorSensorPlugin.php @@ -230,10 +230,9 @@ class DatabaseAggregatorSensorPlugin extends DatabaseAggregatorSensorPluginBase public function buildConfigurationForm(array $form, FormStateInterface $form_state) { $form = parent::buildConfigurationForm($form, $form_state); - $settings = $this->sensorConfig->getSettings(); $form['table'] = array( '#type' => 'textfield', - '#default_value' => $settings['table'], + '#default_value' => $this->sensorConfig->getSetting('table'), '#maxlength' => 255, '#title' => t('Table'), '#required' => TRUE, @@ -264,7 +263,7 @@ class DatabaseAggregatorSensorPlugin extends DatabaseAggregatorSensorPluginBase ); // Fill the conditions table with keys and values for each condition. - $conditions = $settings['conditions']; + $conditions = $this->sensorConfig->getSetting('conditions'); if (!$form_state->has('conditions_rows')) { $form_state->set('conditions_rows', count($conditions) + 1); @@ -341,7 +340,7 @@ class DatabaseAggregatorSensorPlugin extends DatabaseAggregatorSensorPluginBase ); // Fill the fields table with verbose fields to filter the output. - $fields = $settings['verbose_fields']; + $fields = $this->sensorConfig->getSetting('verbose_fields'); if (!$form_state->has('fields_rows')) { $form_state->set('fields_rows', count($fields) + 1); @@ -406,25 +405,23 @@ class DatabaseAggregatorSensorPlugin extends DatabaseAggregatorSensorPluginBase public function submitConfigurationForm(array &$form, FormStateInterface $form_state) { parent::submitConfigurationForm($form, $form_state); - /** @var \Drupal\monitoring\Form\SensorForm $sensor_form */ - $sensor_form = $form_state->getFormObject(); - /** @var \Drupal\monitoring\SensorConfigInterface $sensor_config */ - $sensor_config = $sensor_form->getEntity(); - $settings = $sensor_config->getSettings(); - // Cleanup conditions, remove empty. - $settings['conditions'] = []; + $conditions = []; foreach ($form_state->getValue('conditions') as $key => $condition) { if (!empty($condition['field'])) { - $settings['conditions'][] = $condition; + $conditions[] = $condition; } } - $settings['verbose_fields'] = []; - foreach ($form_state->getValue('verbose_fields') as $delta => $field) { + + $verbose_fields = []; + foreach ($form_state->getValue('verbose_fields') as $field) { if (!empty($field)) { - $settings['verbose_fields'][] = $field; + $verbose_fields[] = $field; } } + $settings = $this->sensorConfig->getSettings(); + $settings['conditions'] = $conditions; + $settings['verbose_fields'] = $verbose_fields; $this->sensorConfig->set('settings', $settings); } @@ -498,9 +495,15 @@ class DatabaseAggregatorSensorPlugin extends DatabaseAggregatorSensorPluginBase /** @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)) { - $form_state->setErrorByName('settings][table', t('The table %table does not exist in the database %database', ['%table' => $table, '%database' => $database->getConnectionOptions()['database']])); - return; + 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', @@ -517,22 +520,25 @@ class DatabaseAggregatorSensorPlugin extends DatabaseAggregatorSensorPluginBase $fields = $form_state->getValue('verbose_fields'); $new_fields = []; foreach ($fields as $key => $field) { + $query = $database->select($table); $field_name = $field['field']; if (!empty($field_name)) { + $query->addField($table, $field_name); + $new_fields[] = (array_pop($fields[$key])); if (!$database->schema()->fieldExists($table, $field_name)) { - $query = $database->select($table); - $query->addField($table, $field_name); - try{ + try { $query->range(0, 1)->execute(); } catch (\Exception $e) { - $form_state->setErrorByName('verbose_fields][' . $key . '][field', t('The specified field "%name" does not exist in table "%table".', ['%name' => $field_name, '%table' => $table])); + + $form_state->setErrorByName("verbose_fields][$key][field", t('The field %field does not exist in the table "%table".', ['%field' => $new_fields[$key], '%table' => $table])); + unset($new_fields[$key]); continue; } } - $new_fields[] = (array_pop($fields[$key])); } } + $form_state->setValue('verbose_fields', $new_fields); }