You are here

public function DefaultParser::parse in Drupal 8

Same name and namespace in other branches
  1. 9 core/modules/aggregator/src/Plugin/aggregator/parser/DefaultParser.php \Drupal\aggregator\Plugin\aggregator\parser\DefaultParser::parse()

Parses feed data.

Parameters

\Drupal\aggregator\FeedInterface $feed: An object describing the resource to be parsed. $feed->source_string->value contains the raw feed data. Parse the data and add the following properties to the $feed object:

  • description: The human-readable description of the feed.
  • link: A full URL that directly relates to the feed.
  • image: An image URL used to display an image of the feed.
  • etag: An entity tag from the HTTP header used for cache validation to determine if the content has been changed.
  • modified: The UNIX timestamp when the feed was last modified.
  • items: An array of feed items. The common format for a single feed item is an associative array containing:

    • title: The human-readable title of the feed item.
    • description: The full body text of the item or a summary.
    • timestamp: The UNIX timestamp when the feed item was last published.
    • author: The author of the feed item.
    • guid: The global unique identifier (GUID) string that uniquely identifies the item. If not available, the link is used to identify the item.
    • link: A full URL to the individual feed item.

Return value

bool TRUE if parsing was successful, FALSE otherwise.

Overrides ParserInterface::parse

1 call to DefaultParser::parse()
TestParser::parse in core/modules/aggregator/tests/modules/aggregator_test/src/Plugin/aggregator/parser/TestParser.php
Implements \Drupal\aggregator\Plugin\ParserInterface::parse().
1 method overrides DefaultParser::parse()
TestParser::parse in core/modules/aggregator/tests/modules/aggregator_test/src/Plugin/aggregator/parser/TestParser.php
Implements \Drupal\aggregator\Plugin\ParserInterface::parse().

File

core/modules/aggregator/src/Plugin/aggregator/parser/DefaultParser.php, line 29

Class

DefaultParser
Defines a default parser implementation.

Namespace

Drupal\aggregator\Plugin\aggregator\parser

Code

public function parse(FeedInterface $feed) {

  // Set our bridge extension manager to Laminas 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']);
  }

  // Initialize items array.
  $feed->items = [];
  foreach ($channel as $item) {

    // Reset the parsed item.
    $parsed_item = [];

    // Move the values to an array as expected by processors.
    $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();
    }

    // Store on $feed object. This is where processors will look for parsed items.
    $feed->items[] = $parsed_item;
  }
  return TRUE;
}