You are here

public function DefaultProcessor::process in Drupal 9

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

Processes feed data.

Parameters

\Drupal\aggregator\FeedInterface $feed: A feed object representing the resource to be processed. $feed->items contains an array of feed items downloaded and parsed at the parsing stage. See \Drupal\aggregator\Plugin\FetcherInterface::parse() for the basic format of a single item in the $feed->items array. For the exact format refer to the particular parser in use.

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();
  }
}