You are here

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

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\SensorPlugin
View 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

Namesort descending Modifiers Type Description Overrides
MessengerTrait::$messenger protected property The messenger. 29
MessengerTrait::messenger public function Gets the messenger. 29
MessengerTrait::setMessenger public function Sets the messenger.
SensorPluginBase::$pluginDefinition protected property The plugin implementation definition.
SensorPluginBase::$pluginId protected property The plugin_id.
SensorPluginBase::$sensorConfig protected property Current sensor config object.
SensorPluginBase::$services protected property
SensorPluginBase::addService public function Service setter. Overrides SensorPluginInterface::addService
SensorPluginBase::calculateDependencies public function Calculates dependencies for the configured plugin. Overrides SensorPluginInterface::calculateDependencies 4
SensorPluginBase::create public static function Creates an instance of the sensor with config. Overrides SensorPluginInterface::create 7
SensorPluginBase::getConfigurableValueType public function Configurable value type. Overrides SensorPluginInterface::getConfigurableValueType
SensorPluginBase::getPluginDefinition public function Gets the definition of the plugin implementation. Overrides PluginInspectionInterface::getPluginDefinition
SensorPluginBase::getPluginId public function Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface::getPluginId
SensorPluginBase::getSensorId public function Gets sensor name (not the label). Overrides SensorPluginInterface::getSensorId
SensorPluginBase::getService public function @todo: Replace with injection Overrides SensorPluginInterface::getService
SensorPluginBase::isEnabled public function Determines if sensor is enabled. Overrides SensorPluginInterface::isEnabled
SensorPluginBase::submitConfigurationForm public function Form submission handler. Overrides PluginFormInterface::submitConfigurationForm 3
SensorPluginBase::validateConfigurationForm public function Form validation handler. Overrides PluginFormInterface::validateConfigurationForm 2
SensorPluginBase::__construct function Instantiates a sensor object. 8
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.
ViewDisplayAggregatorSensorPlugin::$configurableValueType protected property Allows plugins to control if the value type can be configured. Overrides SensorPluginBase::$configurableValueType
ViewDisplayAggregatorSensorPlugin::buildConfigurationForm public function Form constructor. Overrides SensorPluginBase::buildConfigurationForm
ViewDisplayAggregatorSensorPlugin::getDefaultConfiguration public function Default configuration for a sensor. Overrides SensorPluginBase::getDefaultConfiguration
ViewDisplayAggregatorSensorPlugin::getDisplayOptions protected function Gets the display list for selected view.
ViewDisplayAggregatorSensorPlugin::getViewsOptions protected function Gets the available views.
ViewDisplayAggregatorSensorPlugin::resultVerbose public function Provide additional info about sensor call. Overrides ExtendedInfoSensorPluginInterface::resultVerbose
ViewDisplayAggregatorSensorPlugin::runSensor public function Runs the sensor, updating $sensor_result. Overrides SensorPluginInterface::runSensor
ViewDisplayAggregatorSensorPlugin::submitSelectView public function Handles submit call when view type is selected.