You are here

public function DatabaseAggregatorSensorPlugin::buildConfigurationForm in Monitoring 8

Adds UI for variables table, conditions and keys.

Overrides DatabaseAggregatorSensorPluginBase::buildConfigurationForm

1 call to DatabaseAggregatorSensorPlugin::buildConfigurationForm()
TemporaryFilesUsagesSensorPlugin::buildConfigurationForm in src/Plugin/monitoring/SensorPlugin/TemporaryFilesUsagesSensorPlugin.php
Adds UI for variables table, conditions and keys.
1 method overrides DatabaseAggregatorSensorPlugin::buildConfigurationForm()
TemporaryFilesUsagesSensorPlugin::buildConfigurationForm in src/Plugin/monitoring/SensorPlugin/TemporaryFilesUsagesSensorPlugin.php
Adds UI for variables table, conditions and keys.

File

src/Plugin/monitoring/SensorPlugin/DatabaseAggregatorSensorPlugin.php, line 402
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 buildConfigurationForm(array $form, FormStateInterface $form_state) {
  $form = parent::buildConfigurationForm($form, $form_state);
  $form['table'] = array(
    '#type' => 'textfield',
    '#default_value' => $this->sensorConfig
      ->getSetting('table'),
    '#maxlength' => 255,
    '#title' => t('Table'),
    '#required' => TRUE,
    '#access' => $this->configurableTable,
  );

  // Add conditions.
  // Fieldset for sensor list elements.
  $form['conditions_table'] = array(
    '#type' => 'fieldset',
    '#title' => t('Conditions'),
    '#prefix' => '<div id="selected-conditions">',
    '#suffix' => '</div>',
    '#tree' => FALSE,
    '#access' => $this->configurableConditions,
  );

  // Table for included sensors.
  $form['conditions_table']['conditions'] = array(
    '#type' => 'table',
    '#tree' => TRUE,
    '#header' => array(
      'field' => t('Field key'),
      'operator' => t('Operator'),
      'value' => t('Value'),
    ),
    '#empty' => t('Add conditions to filter the results.'),
  );

  // Fill the conditions table with keys and values for each condition.
  $conditions = (array) $this->sensorConfig
    ->getSetting('conditions');
  if (!$form_state
    ->has('conditions_rows')) {
    $form_state
      ->set('conditions_rows', count($conditions) + 1);
  }
  for ($i = 0; $i < $form_state
    ->get('conditions_rows'); $i++) {
    $condition = isset($conditions[$i]) ? $conditions[$i] : array();
    $condition += array(
      'field' => '',
      'value' => '',
      'operator' => '=',
    );
    $form['conditions_table']['conditions'][$i] = array(
      'field' => array(
        '#type' => 'textfield',
        '#default_value' => $condition['field'],
        '#title' => t('Field'),
        '#title_display' => 'invisible',
        '#size' => 20,
      ),
      'operator' => array(
        '#type' => 'select',
        '#default_value' => $condition['operator'],
        '#title' => t('Operator'),
        '#title_display' => 'invisible',
        '#options' => $this
          ->getConditionsOperators(),
      ),
      'value' => array(
        '#type' => 'textfield',
        '#default_value' => $condition['value'],
        '#title' => t('Value'),
        '#title_display' => 'invisible',
        '#size' => 40,
      ),
    );
  }

  // Select element for available conditions.
  $form['conditions_table']['condition_add_button'] = array(
    '#type' => 'submit',
    '#value' => t('Add another condition'),
    '#ajax' => array(
      'wrapper' => 'selected-conditions',
      'callback' => array(
        $this,
        'conditionsReplace',
      ),
      'method' => 'replace',
    ),
    '#submit' => array(
      array(
        $this,
        'addConditionSubmit',
      ),
    ),
  );

  // Add a fieldset to filter verbose output by fields.
  $form['output_table'] = array(
    '#type' => 'fieldset',
    '#title' => t('Verbose Fields'),
    '#prefix' => '<div id="selected-output">',
    '#suffix' => '</div>',
    '#tree' => FALSE,
    '#access' => $this->configurableVerboseOutput,
  );

  // Add a table for the fields.
  $form['output_table']['verbose_fields'] = array(
    '#type' => 'table',
    '#tree' => TRUE,
    '#header' => array(
      'field_key' => t('Field key'),
    ),
    '#title' => t('Verbose fields'),
    '#empty' => t('Add keys to display in the verbose output.'),
  );

  // Fill the fields table with verbose fields to filter the output.
  $fields = (array) $this->sensorConfig
    ->getSetting('verbose_fields');
  if (!$form_state
    ->has('fields_rows')) {
    $form_state
      ->set('fields_rows', count($fields) + 1);
  }
  for ($i = 0; $i < $form_state
    ->get('fields_rows'); $i++) {
    $field = isset($fields[$i]) ? $fields[$i] : $i;
    $form['output_table']['verbose_fields'][$field] = array(
      // This table only has one column called 'field_key'.
      'field_key' => array(
        '#type' => 'textfield',
        '#default_value' => is_int($field) ? '' : $field,
        '#size' => 20,
      ),
    );
  }

  // Select element for available fields.
  $form['output_table']['fields_add_button'] = array(
    '#type' => 'submit',
    '#value' => t('Add another field'),
    '#ajax' => array(
      'wrapper' => 'selected-output',
      'callback' => array(
        $this,
        'fieldsReplace',
      ),
      'method' => 'replace',
    ),
    '#submit' => array(
      array(
        $this,
        'addFieldSubmit',
      ),
    ),
  );

  // Enable history results in time aggregation.
  $form['aggregation']['history_status'] = [
    '#type' => 'checkbox',
    '#default_value' => $this->sensorConfig
      ->getSetting('history_status'),
    '#title' => t('Enable history'),
    '#description' => t('Check to show history results.'),
  ];

  // Always show the enable history checkbox if a timestamp field is forced,
  // otherwise add states so it is only visible if something is entered.
  if ($this->configurableTimestampField) {
    $form['aggregation']['history_status']['#states'] = [
      'invisible' => [
        ':input[name="settings[aggregation][time_interval_field]"]' => [
          'value' => '',
        ],
      ],
    ];
  }
  return $form;
}