function AggregatorFeedBlock::build

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

Overrides BlockPluginInterface::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']))) {
        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;
    }
}

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