function AnnounceFetcher::fetch

Same name and namespace in other branches
  1. 11.x core/modules/announcements_feed/src/AnnounceFetcher.php \Drupal\announcements_feed\AnnounceFetcher::fetch()

Fetches the feed either from a local cache or fresh remotely.

The feed follows the "JSON Feed" format:

The structure of an announcement item in the feed is:

  • id: Id.
  • title: Title of the announcement.
  • content_html: Announcement teaser.
  • url: URL
  • date_modified: Last updated timestamp.
  • date_published: Created timestamp.
  • _drupalorg.featured: 1 if featured, 0 if not featured.
  • _drupalorg.version: Target version of Drupal, as a Composer version.

Parameters

bool $force: (optional) Whether to always fetch new items or not. Defaults to FALSE.

Return value

\Drupal\announcements_feed\Announcement[] An array of announcements from the feed relevant to the Drupal version. The array is empty if there were no matching announcements. If an error occurred while fetching/decoding the feed, it is thrown as an exception.

Throws

\Exception

1 call to AnnounceFetcher::fetch()
AnnounceFetcher::fetchIds in core/modules/announcements_feed/src/AnnounceFetcher.php
Fetch ids of announcements.

File

core/modules/announcements_feed/src/AnnounceFetcher.php, line 132

Class

AnnounceFetcher
Service to fetch announcements from the external feed.

Namespace

Drupal\announcements_feed

Code

public function fetch(bool $force = FALSE) : array {
  $announcements = $this->tempStore
    ->get('announcements');
  if ($force || $announcements === NULL) {
    try {
      $feed_content = (string) $this->httpClient
        ->get($this->feedUrl)
        ->getBody();
    } catch (\Exception $e) {
      $this->logger
        ->error(Error::DEFAULT_ERROR_MESSAGE, Error::decodeException($e));
      throw $e;
    }
    $announcements = Json::decode($feed_content);
    if (!isset($announcements['items'])) {
      $this->logger
        ->error('The feed format is not valid.');
      throw new \Exception('Invalid format');
    }
    $announcements = $announcements['items'] ?? [];
    // Ensure that announcements reference drupal.org and are applicable to
    // the current Drupal version.
    $announcements = array_filter($announcements, function (array $announcement) {
      return static::validateUrl($announcement['url'] ?? '') && static::isRelevantItem($announcement['_drupalorg']['version'] ?? '');
    });
    // Save the raw decoded and filtered array to temp store.
    $this->tempStore
      ->setWithExpire('announcements', $announcements, $this->config
      ->get('max_age'));
  }
  // The drupal.org endpoint is sorted by created date in descending order.
  // We will limit the announcements based on the configuration limit.
  $announcements = array_slice($announcements, 0, $this->config
    ->get('limit') ?? 10);
  // For the remaining announcements, put all the featured announcements
  // before the rest.
  uasort($announcements, function ($a, $b) {
    $a_value = (int) $a['_drupalorg']['featured'];
    $b_value = (int) $b['_drupalorg']['featured'];
    if ($a_value == $b_value) {
      return 0;
    }
    return $a_value < $b_value ? -1 : 1;
  });
  // Map the multidimensional array into an array of Announcement objects.
  $announcements = array_map(function ($announcement) {
    return new Announcement($announcement['id'], $announcement['title'], $announcement['url'], $announcement['date_modified'], $announcement['date_published'], $announcement['content_html'], $announcement['_drupalorg']['version'], (bool) $announcement['_drupalorg']['featured']);
  }, $announcements);
  return $announcements;
}

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