class AnnounceRenderer

Same name and namespace in other branches
  1. 11.x core/modules/announcements_feed/src/AnnounceRenderer.php \Drupal\announcements_feed\AnnounceRenderer

Service to render announcements from the external feed.

@internal

Hierarchy

Expanded class hierarchy of AnnounceRenderer

2 files declare their use of AnnounceRenderer
AnnounceBlock.php in core/modules/announcements_feed/src/Plugin/Block/AnnounceBlock.php
AnnounceController.php in core/modules/announcements_feed/src/Controller/AnnounceController.php
1 string reference to 'AnnounceRenderer'
announcements_feed.services.yml in core/modules/announcements_feed/announcements_feed.services.yml
core/modules/announcements_feed/announcements_feed.services.yml
1 service uses AnnounceRenderer
announcements_feed.renderer in core/modules/announcements_feed/announcements_feed.services.yml
Drupal\announcements_feed\AnnounceRenderer

File

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

Namespace

Drupal\announcements_feed
View source
final class AnnounceRenderer {
  use StringTranslationTrait;
  
  /**
   * Constructs an AnnouncementRenderer object.
   *
   * @param \Drupal\announcements_feed\AnnounceFetcher $announceFetcher
   *   The AnnounceFetcher service.
   * @param string $feedLink
   *   The feed url path.
   */
  public function __construct(protected AnnounceFetcher $announceFetcher, protected string $feedLink) {
  }
  
  /**
   * Generates the announcements feed render array.
   *
   * @return array
   *   Render array containing the announcements feed.
   */
  public function render() : array {
    try {
      $announcements = $this->announceFetcher
        ->fetch();
    } catch (\Exception $e) {
      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;
  }

}

Members

Title Sort descending Modifiers Object type Summary Overrides
AnnounceRenderer::render public function Generates the announcements feed render array.
AnnounceRenderer::__construct public function Constructs an AnnouncementRenderer object.
StringTranslationTrait::$stringTranslation protected property The string translation service. 3
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.

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