You are here

public function FileDownloadPopularBlock::build in File Download 8

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

modules/file_download_counter/src/Plugin/Block/FileDownloadPopularBlock.php, line 81

Class

FileDownloadPopularBlock
Provides a 'Popular content' block.

Namespace

Drupal\file_download_counter\Plugin\Block

Code

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