You are here

public function ContentEntityAggregatorSensorPlugin::verboseResultUnaggregated in Monitoring 8

Adds unaggregated verbose output to the render array $output.

Parameters

array &$output: Render array where the result will be added.

1 call to ContentEntityAggregatorSensorPlugin::verboseResultUnaggregated()
ContentEntityAggregatorSensorPlugin::resultVerbose in src/Plugin/monitoring/SensorPlugin/ContentEntityAggregatorSensorPlugin.php
Provide additional info about sensor call.

File

src/Plugin/monitoring/SensorPlugin/ContentEntityAggregatorSensorPlugin.php, line 212
Contains \Drupal\monitoring\Plugin\monitoring\SensorPlugin\ContentEntityAggregatorSensorPlugin.

Class

ContentEntityAggregatorSensorPlugin
Content entity database aggregator.

Namespace

Drupal\monitoring\Plugin\monitoring\SensorPlugin

Code

public function verboseResultUnaggregated(array &$output) {
  $output = [];

  /** @var \Drupal\Core\Field\FieldTypePluginManagerInterface $field_type_manager */
  $field_type_manager = \Drupal::service('plugin.manager.field.field_type');
  Database::startLog('monitoring_ceasp');

  // Fetch the last 10 matching entries, unaggregated.
  $entity_ids = $this
    ->getEntityQuery()
    ->range(0, 10)
    ->execute();

  // Show query.
  $query_log = Database::getLog('monitoring_ceasp')[0];

  // Load entities.
  $entity_type_id = $this->sensorConfig
    ->getSetting('entity_type');
  $entities = $this->entityTypeManager
    ->getStorage($entity_type_id)
    ->loadMultiple($entity_ids);

  // Get the fields to display from the settings.
  $fields = $this->sensorConfig
    ->getSetting('verbose_fields', [
    'id',
    'label',
  ]);

  // Render entities.
  $rows = [];

  /* @var \Drupal\Core\Entity\FieldableEntityInterface $entity */
  foreach ($entities as $id => $entity) {
    $row = [];
    foreach ($fields as $field) {
      switch ($field) {
        case 'id':
          $row[] = $entity
            ->id();
          break;
        case $this
          ->getTimeIntervalField():
          $row[] = \Drupal::service('date.formatter')
            ->format($entity
            ->get($this
            ->getTimeIntervalField())[0]->value, 'short');
          break;
        case 'label':
          $row[] = $entity
            ->hasLinkTemplate('canonical') ? $entity
            ->toLink() : $entity
            ->label();
          break;
        default:

          // Make sure the field exists on this entity.
          if ($entity instanceof FieldableEntityInterface && $entity
            ->hasField($field)) {
            try {

              // Get the main property as a fallback if the field can not be
              // viewed.
              $field_type = $entity
                ->getFieldDefinition($field)
                ->getFieldStorageDefinition()
                ->getType();

              // If the field type has a default formatter, try to view it.
              if (isset($field_type_manager
                ->getDefinition($field_type)['default_formatter'])) {
                $value = $entity->{$field}
                  ->view([
                  'label' => 'hidden',
                ]);
                $row[] = \Drupal::service('renderer')
                  ->renderPlain($value);
              }
              else {

                // Fall back to the main property.
                $property = $entity
                  ->getFieldDefinition($field)
                  ->getFieldStorageDefinition()
                  ->getMainPropertyName();
                $row[] = new HtmlEscapedText($entity->{$field}->{$property});
              }
            } catch (\Exception $e) {

              // Catch any exception and display as an error.
              $this
                ->messenger()
                ->addError(t('Error while trying to display %field: @error', [
                '%field' => $field,
                '@error' => $e
                  ->getMessage(),
              ]));
              $row[] = '';
            }
          }
          else {
            $row[] = '';
          }
          break;
      }
    }
    $rows[] = array(
      'data' => $row,
      'class' => 'entity',
    );
  }
  $header = $this->sensorConfig
    ->getSetting('verbose_fields', [
    'id',
    'label',
  ]);
  $output['entities'] = array(
    '#type' => 'verbose_table_result',
    '#header' => $header,
    '#rows' => $rows,
    '#empty' => t('No matching entities were found.'),
    '#query' => $query_log['query'],
    '#query_args' => $query_log['args'],
  );
  return $output;
}