You are here

public function Comments::buildRenderArray in Dashboards with Layout Builder 8

Same name and namespace in other branches
  1. 2.0.x modules/dashboards_comments/src/Plugin/Dashboard/Comments.php \Drupal\dashboards_comments\Plugin\Dashboard\Comments::buildRenderArray()

Build render array.

Parameters

array $configuration: Plugin configuration.

Return value

array Return render array.

Overrides DashboardBase::buildRenderArray

File

modules/dashboards_comments/src/Plugin/Dashboard/Comments.php, line 89

Class

Comments
Plugin for comment reports.

Namespace

Drupal\dashboards_comments\Plugin\Dashboard

Code

public function buildRenderArray($configuration) : array {
  $stats = [];
  $field = 'comment_count';
  $cache = $this
    ->getCache($field);
  if (!$cache) {
    $query = $this->database
      ->select('node_field_data', 'nfd');
    $query
      ->join('comment_entity_statistics', 'ces', 'ces.entity_id = nfd.nid');
    $query
      ->fields('nfd', [
      'type',
    ]);
    $query
      ->addExpression('SUM(ces.' . $field . ')', 'count');
    $query
      ->groupBy('type');
    $rows = [];
    $stats['types'] = $query
      ->execute()
      ->fetchAllAssoc('type');
    foreach ($stats['types'] as $type => $count) {
      $rows[] = [
        $type,
        $count->count,
      ];
    }
    $this
      ->setCache($field, $rows, CacheBackendInterface::CACHE_PERMANENT, [
      'comment_list',
    ]);
  }
  else {
    $rows = $cache->data;
  }
  $this
    ->setLabels([
    $this
      ->t('Node Type'),
    $this
      ->t('Count'),
  ]);
  $this
    ->setRows($rows);
  $build = $this
    ->renderChart($configuration);
  $build['#cache'] = [
    'tags' => [
      'node_list',
    ],
  ];
  return $build;
}