You are here

public static function RssNews::readSource in Dashboards with Layout Builder 8

Same name and namespace in other branches
  1. 2.0.x src/Plugin/Dashboard/RssNews.php \Drupal\dashboards\Plugin\Dashboard\RssNews::readSource()

Fetch rss items.

Parameters

string $plugin_id: Plugin id for cache.

string $uri: URI to fetch.

Return value

array Feed items.

1 call to RssNews::readSource()
RssNews::lazyBuild in src/Plugin/Dashboard/RssNews.php
Lazy builder callback.

File

src/Plugin/Dashboard/RssNews.php, line 41

Class

RssNews
Show account info.

Namespace

Drupal\dashboards\Plugin\Dashboard

Code

public static function readSource($plugin_id, $uri) : array {
  $cache = \Drupal::service('dashboards.cache');
  $cid = $plugin_id . ':' . md5($uri);
  if (!($data = $cache
    ->get($cid))) {
    Reader::setExtensionManager(\Drupal::service('feed.bridge.reader'));
    $client = \Drupal::httpClient();
    $response = $client
      ->request('GET', $uri);
    $channel = Reader::importString($response
      ->getBody()
      ->getContents());
    $items = [];
    foreach ($channel as $item) {
      $items[] = [
        'title' => $item
          ->getTitle(),
        'link' => $item
          ->getLink(),
        'description' => $item
          ->getDescription(),
        'date' => $item
          ->getDateModified()
          ->format(\DateTime::ISO8601),
      ];
    }
    $cache
      ->set($cid, $items, time() + static::CACHE_TIME);
    return $items;
  }
  return $data->data;
}