You are here

public function SensorListBuilder::buildForm in Monitoring 8

Form constructor.

Parameters

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

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

Return value

array The form structure.

Overrides FormInterface::buildForm

File

src/SensorListBuilder.php, line 121
Contains \Drupal\monitoring\SensorListBuilder.

Class

SensorListBuilder
Defines a class to build a listing of sensor config entities.

Namespace

Drupal\monitoring

Code

public function buildForm(array $form, FormStateInterface $form_state) {
  $categories = [];
  $options = [];
  $default_value = [];
  $sensor_types = [];

  /** @var \Drupal\monitoring\Entity\SensorConfig $entity */
  foreach ($this
    ->load() as $entity) {
    $row = $this
      ->buildRow($entity);
    $options[$entity
      ->id()] = $row;
    $default_value[$entity
      ->id()] = $entity
      ->isEnabled();

    // Get all sensor categories.
    if (!isset($categories[$entity
      ->getCategory()])) {
      $categories[$entity
        ->getCategory()] = $entity
        ->getCategory();
    }
    $plugin_definition = monitoring_sensor_manager()
      ->getSensorConfigByName($entity
      ->id())
      ->getPlugin()
      ->getPluginDefinition();

    // Get all sensor plugin types.
    $plugin_label_key = $plugin_definition['label']
      ->render();
    if (!isset($sensor_types[$plugin_label_key])) {
      $sensor_types[$plugin_label_key] = $plugin_definition['label'];
    }
  }
  asort($sensor_types);
  $form['filters'] = array(
    '#type' => 'fieldset',
    '#attributes' => array(
      'class' => array(
        'table-filter',
        'js-show',
        'form--inline',
      ),
    ),
    '#weight' => -10,
    '#title' => $this
      ->t('Filter'),
  );
  $form['filters']['sensor_type'] = array(
    '#type' => 'select',
    '#empty_option' => $this
      ->t('- All -'),
    '#title' => $this
      ->t('Sensor type'),
    '#options' => $sensor_types,
    '#attributes' => array(
      'class' => array(
        'table-filter-select-sensor-type',
      ),
    ),
  );
  $form['filters']['category'] = array(
    '#type' => 'select',
    '#empty_option' => $this
      ->t('- All -'),
    '#title' => $this
      ->t('Category'),
    '#options' => $categories,
    '#attributes' => array(
      'class' => array(
        'table-filter-select-category',
      ),
    ),
  );
  $form['filters']['text'] = array(
    '#type' => 'search',
    '#title' => $this
      ->t('Sensor label or sensor id'),
    '#size' => 40,
    '#placeholder' => $this
      ->t('Enter a sensor label or sensor id'),
    '#attributes' => array(
      'class' => array(
        'table-filter-text',
      ),
      'data-table' => '.monitoring-sensor-overview',
      'autocomplete' => 'off',
      'title' => $this
        ->t('Enter a part of the sensor label or sensor id to filter by.'),
    ),
  );
  $form['sensors'] = array(
    '#type' => 'tableselect',
    '#header' => $this
      ->buildHeader(),
    '#options' => $options,
    '#default_value' => $default_value,
    '#attributes' => array(
      'id' => 'monitoring-sensors-config-overview',
    ),
  );
  $form['actions']['#type'] = 'actions';
  $form['actions']['submit'] = array(
    '#type' => 'submit',
    '#value' => $this
      ->t('Update enabled sensors'),
  );
  return $form;
}