You are here

public function ContentEntityAggregatorSensorPlugin::buildConfigurationForm in Monitoring 8

Adds UI for variables entity_type, conditions and verbose_fields.

Overrides DatabaseAggregatorSensorPluginBase::buildConfigurationForm

1 call to ContentEntityAggregatorSensorPlugin::buildConfigurationForm()
CommerceTurnoverSensorPlugin::buildConfigurationForm in src/Plugin/monitoring/SensorPlugin/CommerceTurnoverSensorPlugin.php
Adds additional settings to the sensor configuration form.
1 method overrides ContentEntityAggregatorSensorPlugin::buildConfigurationForm()
CommerceTurnoverSensorPlugin::buildConfigurationForm in src/Plugin/monitoring/SensorPlugin/CommerceTurnoverSensorPlugin.php
Adds additional settings to the sensor configuration form.

File

src/Plugin/monitoring/SensorPlugin/ContentEntityAggregatorSensorPlugin.php, line 322
Contains \Drupal\monitoring\Plugin\monitoring\SensorPlugin\ContentEntityAggregatorSensorPlugin.

Class

ContentEntityAggregatorSensorPlugin
Content entity database aggregator.

Namespace

Drupal\monitoring\Plugin\monitoring\SensorPlugin

Code

public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
  $form = parent::buildConfigurationForm($form, $form_state);
  $settings = $this->sensorConfig
    ->getSettings();
  $options = [];
  foreach ($this->entityTypeManager
    ->getDefinitions() as $id => $entity_type) {
    if ($entity_type
      ->entityClassImplements(FieldableEntityInterface::class)) {
      $options[$id] = $entity_type
        ->getLabel();
    }
  }
  $form['entity_type'] = array(
    '#type' => 'select',
    '#default_value' => $this->sensorConfig
      ->getSetting('entity_type', 'node'),
    '#maxlength' => 255,
    '#options' => $options,
    '#title' => t('Entity Type'),
    '#ajax' => array(
      'callback' => array(
        $this,
        'fieldsReplace',
      ),
      'wrapper' => 'selected-output',
      'method' => 'replace',
    ),
    '#access' => $this->configurableEntityType,
  );
  if (!isset($settings['entity_type'])) {
    $form['entity_type']['#required'] = TRUE;
  }

  // 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,
  );

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

  // Fill the sensors table with form elements for each sensor.
  $conditions = array_values($this->sensorConfig
    ->getSetting('conditions', []));
  if (empty($conditions)) {
    $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' => '=',
    );

    // See operators https://api.drupal.org/api/drupal/includes%21entity.inc/function/EntityFieldQuery%3A%3AaddFieldCondition/7
    $operators = array(
      '=' => t('='),
      '!=' => t('!='),
      '<' => t('<'),
      '=<' => t('=<'),
      '>' => t('>'),
      '>=' => t('>='),
      'STARTS_WITH' => t('STARTS_WITH'),
      'CONTAINS' => t('CONTAINS'),
    );
    $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' => $operators,
      ),
      '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',
      ),
    ),
  );

  // Fill the sensors table with form elements for each sensor.
  $form['verbose_fields'] = array(
    '#type' => 'details',
    '#title' => t('Verbose Output configuration'),
    '#prefix' => '<div id="selected-output">',
    '#suffix' => '</div>',
    '#open' => TRUE,
  );
  $entity_type = $this->entityTypeManager
    ->getDefinition($this->sensorConfig
    ->getSetting('entity_type'));
  $available_fields = array_merge([
    'id',
    'label',
  ], array_keys($this->entityFieldManager
    ->getBaseFieldDefinitions($entity_type
    ->id())));
  sort($available_fields);
  $form['verbose_fields']['#description'] = t('Available Fields for entity type %type: %fields.', [
    '%type' => $entity_type
      ->getLabel(),
    '%fields' => implode(', ', $available_fields),
  ]);

  // Fill the sensors table with form elements for each sensor.
  $fields = $this->sensorConfig
    ->getSetting('verbose_fields', [
    'id',
    'label',
  ]);
  if (!$form_state
    ->has('fields_rows')) {
    $form_state
      ->set('fields_rows', count($fields) + 1);
  }
  for ($i = 0; $i < $form_state
    ->get('fields_rows'); $i++) {
    $form['verbose_fields'][$i] = [
      '#type' => 'textfield',
      '#default_value' => isset($fields[$i]) ? $fields[$i] : '',
      '#maxlength' => 256,
      '#required' => FALSE,
      '#tree' => TRUE,
    ];
  }
  $form['verbose_fields']['field_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',
      ),
    ),
  );
  return $form;
}