You are here

public function StatisticsPopularBlock::build in Drupal 8

Same name and namespace in other branches
  1. 9 core/modules/statistics/src/Plugin/Block/StatisticsPopularBlock.php \Drupal\statistics\Plugin\Block\StatisticsPopularBlock::build()

Builds and returns the renderable array for this block plugin.

If a block should not be rendered because it has no content, then this method must also ensure to return no content: it must then only return an empty array, or an empty array with #cache set (with cacheability metadata indicating the circumstances for it being empty).

Return value

array A renderable array representing the content of the block.

Overrides BlockPluginInterface::build

See also

\Drupal\block\BlockViewBuilder

File

core/modules/statistics/src/Plugin/Block/StatisticsPopularBlock.php, line 152

Class

StatisticsPopularBlock
Provides a 'Popular content' block.

Namespace

Drupal\statistics\Plugin\Block

Code

public function build() {
  $content = [];
  if ($this->configuration['top_day_num'] > 0) {
    $nids = $this->statisticsStorage
      ->fetchAll('daycount', $this->configuration['top_day_num']);
    if ($nids) {
      $content['top_day'] = $this
        ->nodeTitleList($nids, $this
        ->t("Today's:"));
      $content['top_day']['#suffix'] = '<br />';
    }
  }
  if ($this->configuration['top_all_num'] > 0) {
    $nids = $this->statisticsStorage
      ->fetchAll('totalcount', $this->configuration['top_all_num']);
    if ($nids) {
      $content['top_all'] = $this
        ->nodeTitleList($nids, $this
        ->t('All time:'));
      $content['top_all']['#suffix'] = '<br />';
    }
  }
  if ($this->configuration['top_last_num'] > 0) {
    $nids = $this->statisticsStorage
      ->fetchAll('timestamp', $this->configuration['top_last_num']);
    $content['top_last'] = $this
      ->nodeTitleList($nids, $this
      ->t('Last viewed:'));
    $content['top_last']['#suffix'] = '<br />';
  }
  return $content;
}