function AggregatorFeedBlock::build

Same name in other branches
  1. 9 core/modules/aggregator/src/Plugin/Block/AggregatorFeedBlock.php \Drupal\aggregator\Plugin\Block\AggregatorFeedBlock::build()

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'])) {
        $result = $this->itemStorage
            ->getQuery()
            ->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;
        }
    }
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.