DefaultParser.php in Drupal 9
File
core/modules/aggregator/src/Plugin/aggregator/parser/DefaultParser.php
View source
<?php
namespace Drupal\aggregator\Plugin\aggregator\parser;
use Drupal\aggregator\Plugin\ParserInterface;
use Drupal\aggregator\FeedInterface;
use Drupal\Core\Messenger\MessengerTrait;
use Laminas\Feed\Reader\Reader;
use Laminas\Feed\Reader\Exception\ExceptionInterface;
class DefaultParser implements ParserInterface {
use MessengerTrait;
public function parse(FeedInterface $feed) {
Reader::setExtensionManager(\Drupal::service('feed.bridge.reader'));
try {
$channel = Reader::importString($feed->source_string);
} catch (ExceptionInterface $e) {
watchdog_exception('aggregator', $e);
$this
->messenger()
->addError(t('The feed from %site seems to be broken because of error "%error".', [
'%site' => $feed
->label(),
'%error' => $e
->getMessage(),
]));
return FALSE;
}
$feed
->setWebsiteUrl($channel
->getLink());
$feed
->setDescription($channel
->getDescription());
if ($image = $channel
->getImage()) {
$feed
->setImage($image['uri']);
}
$feed->items = [];
foreach ($channel as $item) {
$parsed_item = [];
$parsed_item['title'] = $item
->getTitle();
$parsed_item['guid'] = $item
->getId();
$parsed_item['link'] = $item
->getLink();
$parsed_item['description'] = $item
->getDescription();
$parsed_item['author'] = '';
if ($author = $item
->getAuthor()) {
$parsed_item['author'] = $author['name'];
}
$parsed_item['timestamp'] = '';
if ($date = $item
->getDateModified()) {
$parsed_item['timestamp'] = $date
->getTimestamp();
}
$feed->items[] = $parsed_item;
}
return TRUE;
}
}