You are here

public function SensorForm::form in Monitoring 8

Gets the actual form array to be built.

Overrides EntityForm::form

See also

\Drupal\Core\Entity\EntityForm::processForm()

\Drupal\Core\Entity\EntityForm::afterBuild()

File

src/Form/SensorForm.php, line 29
Contains \Drupal\monitoring\Form\SensorForm.

Class

SensorForm
Sensor settings form controller.

Namespace

Drupal\monitoring\Form

Code

public function form(array $form, FormStateInterface $form_state) {
  $form = parent::form($form, $form_state);
  $sensor_config = $this->entity;
  $form['label'] = array(
    '#type' => 'textfield',
    '#title' => $this
      ->t('Label'),
    '#maxlength' => 255,
    '#default_value' => $sensor_config
      ->getLabel(),
    '#required' => TRUE,
  );
  $form['id'] = array(
    '#type' => 'machine_name',
    '#title' => $this
      ->t('ID'),
    '#maxlength' => 255,
    '#default_value' => $sensor_config
      ->id(),
    '#description' => $this
      ->t("ID of the sensor"),
    '#required' => TRUE,
    '#disabled' => !$sensor_config
      ->isNew(),
    '#machine_name' => array(
      'exists' => 'Drupal\\monitoring\\Entity\\SensorConfig::load',
    ),
  );
  $form['status'] = array(
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Enabled'),
    '#description' => $this
      ->t('Check to have the sensor trigger.'),
    '#default_value' => $sensor_config
      ->status(),
  );
  if ($sensor_config
    ->isNew()) {
    $plugin_types = array();
    foreach (monitoring_sensor_manager()
      ->getDefinitions() as $plugin_id => $definition) {
      if ($definition['addable'] == TRUE) {
        $plugin_types[$plugin_id] = (string) $definition['label'];
      }
    }
    uasort($plugin_types, 'strnatcasecmp');
    $form['plugin_id'] = array(
      '#type' => 'select',
      '#options' => $plugin_types,
      '#title' => $this
        ->t('Sensor Plugin'),
      '#limit_validation_errors' => array(
        array(
          'plugin_id',
        ),
      ),
      '#submit' => array(
        '::submitSelectPlugin',
      ),
      '#required' => TRUE,
      '#executes_submit_callback' => TRUE,
      '#ajax' => array(
        'callback' => '::ajaxReplacePluginSpecificForm',
        'wrapper' => 'monitoring-sensor-plugin',
        'method' => 'replace',
      ),
    );
    $form['select_plugin'] = array(
      '#type' => 'submit',
      '#value' => $this
        ->t('Select sensor'),
      '#limit_validation_errors' => array(
        array(
          'plugin_id',
        ),
      ),
      '#submit' => array(
        '::submitSelectPlugin',
      ),
      '#attributes' => array(
        'class' => array(
          'js-hide',
        ),
      ),
    );
  }
  else {

    // @todo odd name but this can not be set to plugin_id.
    $form['old_plugin_id'] = array(
      '#type' => 'item',
      '#title' => $this
        ->t('Sensor Plugin'),
      '#markup' => (string) monitoring_sensor_manager()
        ->getDefinition($sensor_config->plugin_id)['label'],
    );
  }
  $form['plugin_container'] = array(
    '#type' => 'container',
    '#prefix' => '<div id="monitoring-sensor-plugin">',
    '#suffix' => '</div>',
  );
  if (isset($sensor_config->plugin_id) && ($plugin = $sensor_config
    ->getPlugin())) {
    $form['plugin_container']['settings'] = array(
      '#type' => 'details',
      '#open' => TRUE,
      '#title' => $this
        ->t('Sensor plugin settings'),
      '#tree' => TRUE,
    );
    $form['plugin_container']['settings'] += (array) $plugin
      ->buildConfigurationForm($form['plugin_container']['settings'], $form_state);
    $settings = $sensor_config
      ->getSettings();
    foreach ($settings as $key => $value) {
      if (!isset($form['plugin_container']['settings'][$key])) {
        $form['plugin_container']['settings'][$key] = array(
          '#type' => 'value',
          '#value' => $value,
        );
      }
    }
    $form['plugin_container']['category'] = array(
      '#type' => 'textfield',
      '#title' => $this
        ->t('Category'),
      '#maxlength' => 255,
      '#autocomplete_route_name' => 'monitoring.category_autocomplete',
      '#default_value' => $sensor_config
        ->getCategory(),
    );
    $form['plugin_container']['description'] = array(
      '#type' => 'textfield',
      '#title' => $this
        ->t('Description'),
      '#maxlength' => 255,
      '#default_value' => $sensor_config
        ->getDescription(),
    );
    $form['plugin_container']['caching_time'] = array(
      '#type' => 'number',
      '#title' => $this
        ->t('Cache Time'),
      '#maxlength' => 10,
      '#default_value' => $sensor_config
        ->getCachingTime(),
      '#description' => $this
        ->t("The caching time for the sensor. Empty to disable caching."),
      '#field_suffix' => $this
        ->t('seconds'),
    );
    $value_types = [];
    foreach (monitoring_value_types() as $value_type => $info) {
      $value_types[$value_type] = $info['label'];
    }
    $value_type = $sensor_config
      ->getValueType();
    $form['plugin_container']['value_type'] = array(
      '#type' => 'select',
      '#title' => $this
        ->t('Expected value type'),
      '#options' => $value_types,
      '#default_value' => $value_type,
      '#limit_validation_errors' => array(
        array(
          'value_type',
        ),
      ),
      '#submit' => array(
        '::submitSelectPlugin',
      ),
      '#required' => TRUE,
      '#executes_submit_callback' => TRUE,
      '#ajax' => array(
        'callback' => '::ajaxReplacePluginSpecificForm',
        'wrapper' => 'monitoring-sensor-plugin',
        'method' => 'replace',
      ),
      '#access' => $sensor_config
        ->getPlugin()
        ->getConfigurableValueType(),
    );
    $form['plugin_container']['value_label'] = array(
      '#type' => 'textfield',
      '#title' => $this
        ->t('Value Label'),
      '#maxlength' => 255,
      '#default_value' => $sensor_config
        ->getValueLabel(),
      '#description' => $this
        ->t("The value label represents the units of the sensor value."),
      '#access' => $value_type != 'no_value',
    );
    if ($this->entity
      ->isNumeric()) {
      $form['plugin_container']['thresholds'] = array(
        '#type' => 'details',
        '#title' => $this
          ->t('Sensor thresholds'),
        '#description' => $this
          ->t('Here you can set limit values that switch the sensor to a given status.'),
        '#prefix' => '<div id="monitoring-sensor-thresholds">',
        '#suffix' => '</div>',
        '#open' => TRUE,
        '#tree' => TRUE,
      );
      $this
        ->thresholdsForm($form, $form_state);
    }
  }
  return $form;
}