You are here

public function AggregatorFeedBlock::build in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/aggregator/src/Plugin/Block/AggregatorFeedBlock.php \Drupal\aggregator\Plugin\Block\AggregatorFeedBlock::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/aggregator/src/Plugin/Block/AggregatorFeedBlock.php, line 128

Class

AggregatorFeedBlock
Provides an 'Aggregator feed' block with the latest items from the feed.

Namespace

Drupal\aggregator\Plugin\Block

Code

public function build() {

  // Load the selected feed.
  if (!($feed = $this->feedStorage
    ->load($this->configuration['feed']))) {
    return [];
  }
  $result = $this->itemStorage
    ->getQuery()
    ->accessCheck(TRUE)
    ->condition('fid', $feed
    ->id())
    ->range(0, $this->configuration['block_count'])
    ->sort('timestamp', 'DESC')
    ->sort('iid', 'DESC')
    ->execute();
  if ($result) {

    // Only display the block if there are items to show.
    $items = $this->itemStorage
      ->loadMultiple($result);
    $build['list'] = [
      '#theme' => 'item_list',
      '#items' => [],
    ];
    foreach ($items as $item) {
      $build['list']['#items'][$item
        ->id()] = [
        '#type' => 'link',
        '#url' => $item
          ->toUrl(),
        '#title' => $item
          ->label(),
      ];
    }
    $build['more_link'] = [
      '#type' => 'more_link',
      '#url' => $feed
        ->toUrl(),
      '#attributes' => [
        'title' => $this
          ->t("View this feed's recent news."),
      ],
    ];
    return $build;
  }
}