View source
<?php
namespace Drupal\monitoring\Plugin\monitoring\SensorPlugin;
use Drupal\Core\Form\FormStateInterface;
use Drupal\monitoring\Result\SensorResultInterface;
use Drupal\monitoring\SensorPlugin\ExtendedInfoSensorPluginInterface;
use Drupal\monitoring\SensorPlugin\SensorPluginBase;
use Drupal\views\Views;
class ViewDisplayAggregatorSensorPlugin extends SensorPluginBase implements ExtendedInfoSensorPluginInterface {
protected $configurableValueType = FALSE;
public function resultVerbose(SensorResultInterface $result) {
$output = [];
$view_executable = Views::getView($this->sensorConfig
->getSetting('view'));
$display_id = $this->sensorConfig
->getSetting('display');
$view_executable
->setDisplay($display_id);
$view_executable
->initDisplay();
$view_executable
->setAjaxEnabled(TRUE);
$display = $view_executable->displayHandlers
->get($display_id);
$display
->setOption('use_ajax', TRUE);
$preview = $view_executable
->preview($display_id);
$query = $view_executable
->getQuery()
->query();
$arguments = $query
->arguments();
$output['query'] = array(
'#type' => 'item',
'#title' => t('Query'),
'#markup' => '<pre>' . $query . '</pre>',
);
$output['arguments'] = array(
'#type' => 'item',
'#title' => t('Arguments'),
'#markup' => '<pre>' . var_export($arguments, TRUE) . '</pre>',
);
$output['view'] = array(
'#type' => 'fieldset',
'#title' => t('View preview'),
);
$output['view']['preview'] = $preview;
return $output;
}
public function runSensor(SensorResultInterface $result) {
$view_executable = Views::getView($this->sensorConfig
->getSetting('view'));
$view_executable
->preview($this->sensorConfig
->getSetting('display'));
$records_count = $view_executable->total_rows;
$result
->setValue($records_count);
}
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
$form = parent::buildConfigurationForm($form, $form_state);
$form['view'] = array(
'#type' => 'select',
'#options' => $this
->getViewsOptions(),
'#title' => t('View'),
'#default_value' => $this->sensorConfig
->getSetting('view'),
'#required' => TRUE,
'#limit_validation_errors' => array(
array(
'settings',
'view',
),
),
'#submit' => array(
array(
$this,
'submitSelectView',
),
),
'#executes_submit_callback' => TRUE,
'#ajax' => array(
'callback' => '::ajaxReplacePluginSpecificForm',
'wrapper' => 'monitoring-sensor-plugin',
'method' => 'replace',
),
);
$form['view_update'] = array(
'#type' => 'submit',
'#value' => t('Select view'),
'#limit_validation_errors' => array(
array(
'settings',
'view',
),
),
'#submit' => array(
array(
$this,
'submitSelectView',
),
),
'#attributes' => array(
'class' => array(
'js-hide',
),
),
);
if ($view = $this->sensorConfig
->getSetting('view')) {
$form['display'] = array(
'#type' => 'select',
'#title' => t('Display'),
'#required' => TRUE,
'#options' => $this
->getDisplayOptions($view),
'#default_value' => $this->sensorConfig
->getSetting('display'),
);
}
return $form;
}
protected function getViewsOptions() {
$options = [];
$views = Views::getAllViews();
foreach ($views as $view) {
$options[$view
->id()] = $view
->label();
}
return $options;
}
protected function getDisplayOptions($view_id) {
$options = [];
$displays = Views::getView($view_id)->storage
->get('display');
foreach ($displays as $display) {
$options[$display['id']] = $display['display_title'];
}
return $options;
}
public function submitSelectView(array $form, FormStateInterface $form_state) {
$form_state
->setRebuild();
}
public function getDefaultConfiguration() {
$default_config = array(
'value_type' => 'number',
);
return $default_config;
}
}