class DefaultParser

Same name in other branches
  1. 8.9.x core/modules/aggregator/src/Plugin/aggregator/parser/DefaultParser.php \Drupal\aggregator\Plugin\aggregator\parser\DefaultParser

Defines a default parser implementation.

Parses RSS, Atom and RDF feeds.

Plugin annotation


@AggregatorParser(
  id = "aggregator",
  title = @Translation("Default parser"),
  description = @Translation("Default parser for RSS, Atom and RDF feeds.")
)

Hierarchy

Expanded class hierarchy of DefaultParser

1 file declares its use of DefaultParser
TestParser.php in core/modules/aggregator/tests/modules/aggregator_test/src/Plugin/aggregator/parser/TestParser.php

File

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

Namespace

Drupal\aggregator\Plugin\aggregator\parser
View source
class DefaultParser implements ParserInterface {
    use StringTranslationTrait;
    use MessengerTrait;
    
    /**
     * {@inheritdoc}
     */
    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($this->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;
    }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title Overrides
DefaultParser::parse public function Parses feed data. Overrides ParserInterface::parse 1
MessengerTrait::$messenger protected property The messenger. 17
MessengerTrait::messenger public function Gets the messenger. 17
MessengerTrait::setMessenger public function Sets the messenger.
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.