function DefaultProcessor::process

Same name in other branches
  1. 9 core/modules/aggregator/src/Plugin/aggregator/processor/DefaultProcessor.php \Drupal\aggregator\Plugin\aggregator\processor\DefaultProcessor::process()

Overrides ProcessorInterface::process

File

core/modules/aggregator/src/Plugin/aggregator/processor/DefaultProcessor.php, line 185

Class

DefaultProcessor
Defines a default processor implementation.

Namespace

Drupal\aggregator\Plugin\aggregator\processor

Code

public function process(FeedInterface $feed) {
    if (!is_array($feed->items)) {
        return;
    }
    foreach ($feed->items as $item) {
        // @todo: The default entity view builder always returns an empty
        //   array, which is ignored in aggregator_save_item() currently. Should
        //   probably be fixed.
        if (empty($item['title'])) {
            continue;
        }
        // Save this item. Try to avoid duplicate entries as much as possible. If
        // we find a duplicate entry, we resolve it and pass along its ID is such
        // that we can update it if needed.
        if (!empty($item['guid'])) {
            $values = [
                'fid' => $feed->id(),
                'guid' => $item['guid'],
            ];
        }
        elseif ($item['link'] && $item['link'] != $feed->link && $item['link'] != $feed->url) {
            $values = [
                'fid' => $feed->id(),
                'link' => $item['link'],
            ];
        }
        else {
            $values = [
                'fid' => $feed->id(),
                'title' => $item['title'],
            ];
        }
        // Try to load an existing entry.
        if ($entry = $this->itemStorage
            ->loadByProperties($values)) {
            $entry = reset($entry);
        }
        else {
            $entry = Item::create([
                'langcode' => $feed->language()
                    ->getId(),
            ]);
        }
        if ($item['timestamp']) {
            $entry->setPostedTime($item['timestamp']);
        }
        // Make sure the item title and author fit in the 255 varchar column.
        $entry->setTitle(Unicode::truncate($item['title'], 255, TRUE, TRUE));
        $entry->setAuthor(Unicode::truncate($item['author'], 255, TRUE, TRUE));
        $entry->setFeedId($feed->id());
        $entry->setLink($item['link']);
        $entry->setGuid($item['guid']);
        $description = '';
        if (!empty($item['description'])) {
            $description = $item['description'];
        }
        $entry->setDescription($description);
        $entry->save();
    }
}

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