function AnnounceRenderer::render

Same name in other branches
  1. 10 core/modules/announcements_feed/src/AnnounceRenderer.php \Drupal\announcements_feed\AnnounceRenderer::render()

Generates the announcements feed render array.

Return value

array Render array containing the announcements feed.

File

core/modules/announcements_feed/src/AnnounceRenderer.php, line 38

Class

AnnounceRenderer
Service to render announcements from the external feed.

Namespace

Drupal\announcements_feed

Code

public function render() : array {
    try {
        $announcements = $this->announceFetcher
            ->fetch();
    } catch (\Exception) {
        return [
            '#theme' => 'status_messages',
            '#message_list' => [
                'error' => [
                    $this->t('An error occurred while parsing the announcements feed, check the logs for more information.'),
                ],
            ],
            '#status_headings' => [
                'error' => $this->t('Error Message'),
            ],
        ];
    }
    $build = [];
    foreach ($announcements as $announcement) {
        $key = $announcement->featured ? '#featured' : '#standard';
        $build[$key][] = $announcement;
    }
    $build += [
        '#theme' => 'announcements_feed',
        '#count' => count($announcements),
        '#feed_link' => $this->feedLink,
        '#cache' => [
            'contexts' => [
                'url.query_args:_wrapper_format',
            ],
            'tags' => [
                'announcements_feed:feed',
            ],
        ],
        '#attached' => [
            'library' => [
                'announcements_feed/drupal.announcements_feed.dialog',
            ],
        ],
    ];
    return $build;
}

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