class ViewDisplayAggregatorSensorPlugin in Monitoring 8
Execute a view display and count the results.
Plugin annotation
@SensorPlugin(
id = "view_display_aggregator",
label = @Translation("View Display Aggregator"),
description = @Translation("Execute a view display and count the results."),
provider = "views",
addable = TRUE
)
Hierarchy
- class \Drupal\monitoring\SensorPlugin\SensorPluginBase implements SensorPluginInterface uses MessengerTrait, StringTranslationTrait
- class \Drupal\monitoring\Plugin\monitoring\SensorPlugin\ViewDisplayAggregatorSensorPlugin implements ExtendedInfoSensorPluginInterface
Expanded class hierarchy of ViewDisplayAggregatorSensorPlugin
File
- src/
Plugin/ monitoring/ SensorPlugin/ ViewDisplayAggregatorSensorPlugin.php, line 26 - Contains \Drupal\monitoring\Plugin\monitoring\SensorPlugin\ViewDisplayAggregatorSensorPlugin.
Namespace
Drupal\monitoring\Plugin\monitoring\SensorPluginView source
class ViewDisplayAggregatorSensorPlugin extends SensorPluginBase implements ExtendedInfoSensorPluginInterface {
/**
* {@inheritdoc}
*/
protected $configurableValueType = FALSE;
/**
* {@inheritdoc}
*/
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);
// Force ajax mode for the view.
$display = $view_executable->displayHandlers
->get($display_id);
$display
->setOption('use_ajax', TRUE);
// Get the preview of the view for current display.
$preview = $view_executable
->preview($display_id);
// Get the query and arguments of the view.
$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;
}
/**
* {@inheritdoc}
*/
public function runSensor(SensorResultInterface $result) {
$view_executable = Views::getView($this->sensorConfig
->getSetting('view'));
// Execute the view query and get the total rows.
$view_executable
->preview($this->sensorConfig
->getSetting('display'));
$records_count = $view_executable->total_rows;
$result
->setValue($records_count);
}
/**
* {@inheritdoc}
*/
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
$form = parent::buildConfigurationForm($form, $form_state);
// View selection.
$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',
),
),
);
// Show display selection if a view is selected.
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;
}
/**
* Gets the available views.
*
* @return array
* Available views list.
*/
protected function getViewsOptions() {
$options = [];
$views = Views::getAllViews();
foreach ($views as $view) {
$options[$view
->id()] = $view
->label();
}
return $options;
}
/**
* Gets the display list for selected view.
*
* @param string $view_id
* Selected view.
*
* @return array
* Available displays list.
*/
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;
}
/**
* Handles submit call when view type is selected.
*/
public function submitSelectView(array $form, FormStateInterface $form_state) {
$form_state
->setRebuild();
}
/**
* {@inheritdoc}
*/
public function getDefaultConfiguration() {
$default_config = array(
'value_type' => 'number',
);
return $default_config;
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
MessengerTrait:: |
protected | property | The messenger. | 29 |
MessengerTrait:: |
public | function | Gets the messenger. | 29 |
MessengerTrait:: |
public | function | Sets the messenger. | |
SensorPluginBase:: |
protected | property | The plugin implementation definition. | |
SensorPluginBase:: |
protected | property | The plugin_id. | |
SensorPluginBase:: |
protected | property | Current sensor config object. | |
SensorPluginBase:: |
protected | property | ||
SensorPluginBase:: |
public | function |
Service setter. Overrides SensorPluginInterface:: |
|
SensorPluginBase:: |
public | function |
Calculates dependencies for the configured plugin. Overrides SensorPluginInterface:: |
4 |
SensorPluginBase:: |
public static | function |
Creates an instance of the sensor with config. Overrides SensorPluginInterface:: |
7 |
SensorPluginBase:: |
public | function |
Configurable value type. Overrides SensorPluginInterface:: |
|
SensorPluginBase:: |
public | function |
Gets the definition of the plugin implementation. Overrides PluginInspectionInterface:: |
|
SensorPluginBase:: |
public | function |
Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface:: |
|
SensorPluginBase:: |
public | function |
Gets sensor name (not the label). Overrides SensorPluginInterface:: |
|
SensorPluginBase:: |
public | function |
@todo: Replace with injection Overrides SensorPluginInterface:: |
|
SensorPluginBase:: |
public | function |
Determines if sensor is enabled. Overrides SensorPluginInterface:: |
|
SensorPluginBase:: |
public | function |
Form submission handler. Overrides PluginFormInterface:: |
3 |
SensorPluginBase:: |
public | function |
Form validation handler. Overrides PluginFormInterface:: |
2 |
SensorPluginBase:: |
function | Instantiates a sensor object. | 8 | |
StringTranslationTrait:: |
protected | property | The string translation service. | 1 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. | |
ViewDisplayAggregatorSensorPlugin:: |
protected | property |
Allows plugins to control if the value type can be configured. Overrides SensorPluginBase:: |
|
ViewDisplayAggregatorSensorPlugin:: |
public | function |
Form constructor. Overrides SensorPluginBase:: |
|
ViewDisplayAggregatorSensorPlugin:: |
public | function |
Default configuration for a sensor. Overrides SensorPluginBase:: |
|
ViewDisplayAggregatorSensorPlugin:: |
protected | function | Gets the display list for selected view. | |
ViewDisplayAggregatorSensorPlugin:: |
protected | function | Gets the available views. | |
ViewDisplayAggregatorSensorPlugin:: |
public | function |
Provide additional info about sensor call. Overrides ExtendedInfoSensorPluginInterface:: |
|
ViewDisplayAggregatorSensorPlugin:: |
public | function |
Runs the sensor, updating $sensor_result. Overrides SensorPluginInterface:: |
|
ViewDisplayAggregatorSensorPlugin:: |
public | function | Handles submit call when view type is selected. |