ConfigValueSensorPlugin.php in Monitoring 8
File
src/Plugin/monitoring/SensorPlugin/ConfigValueSensorPlugin.php
View source
<?php
namespace Drupal\monitoring\Plugin\monitoring\SensorPlugin;
use Drupal\Core\Entity\DependencyTrait;
use Drupal\Core\Form\FormStateInterface;
use Drupal\monitoring\SensorPlugin\ValueComparisonSensorPluginBase;
class ConfigValueSensorPlugin extends ValueComparisonSensorPluginBase {
use DependencyTrait;
protected function getValueDescription() {
return t('The expected value of config %key, current value: %actVal', array(
'%key' => $this->sensorConfig
->getSetting('config') . ':' . $this->sensorConfig
->getSetting('key'),
'%actVal' => $this
->getValueText(),
));
}
protected function getValue() {
$config = $this
->getConfig($this->sensorConfig
->getSetting('config'));
$key = $this->sensorConfig
->getSetting('key');
if (empty($key)) {
return NULL;
}
return $config
->get($key);
}
protected function getConfig($name) {
return $this
->getService('config.factory')
->get($name);
}
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
$form = parent::buildConfigurationForm($form, $form_state);
$form['config'] = array(
'#type' => 'textfield',
'#default_value' => $this->sensorConfig
->getSetting('config') ? $this->sensorConfig
->getSetting('config') : '',
'#autocomplete_route_name' => 'monitoring.config_autocomplete',
'#maxlength' => 255,
'#title' => t('Config Object'),
'#required' => TRUE,
'#weight' => -1,
);
$form['key'] = array(
'#type' => 'textfield',
'#default_value' => $this->sensorConfig
->getSetting('key') ? $this->sensorConfig
->getSetting('key') : '',
'#maxlength' => 255,
'#title' => t('Key'),
'#required' => TRUE,
'#weight' => -1,
);
return $form;
}
public function calculateDependencies() {
$config_object = $this->sensorConfig
->getSetting('config');
$config = \Drupal::config($config_object);
if (isset($config) && isset($config
->getOriginal()['dependencies'])) {
$this
->addDependency('config', $config_object);
}
$this
->addDependency('module', strtok($config_object, '.'));
return $this->dependencies;
}
}