View source
<?php
namespace Drupal\feeds\Feeds\Parser;
use Drupal\feeds\Component\GenericOpmlParser;
use Drupal\feeds\Exception\EmptyFeedException;
use Drupal\feeds\FeedInterface;
use Drupal\feeds\Feeds\Item\OpmlItem;
use Drupal\feeds\Plugin\Type\Parser\ParserInterface;
use Drupal\feeds\Plugin\Type\PluginBase;
use Drupal\feeds\Result\FetcherResultInterface;
use Drupal\feeds\Result\ParserResult;
use Drupal\feeds\StateInterface;
class OpmlParser extends PluginBase implements ParserInterface {
public function parse(FeedInterface $feed, FetcherResultInterface $fetcher_result, StateInterface $state) {
$raw = $fetcher_result
->getRaw();
if (!strlen(trim($raw))) {
throw new EmptyFeedException();
}
$result = new ParserResult();
$parser = new GenericOpmlParser($fetcher_result
->getRaw());
$opml = $parser
->parse(TRUE);
foreach ($this
->getItems($opml['outlines']) as $item) {
$item
->set('feed_title', $opml['head']['#title']);
$result
->addItem($item);
}
return $result;
}
protected function getItems(array $outlines, array $categories = []) {
$items = [];
foreach ($outlines as $outline) {
$outline += [
'#title' => '',
'#text' => '',
'#xmlurl' => '',
'#htmlurl' => '',
'outlines' => [],
];
$item = new OpmlItem();
if ($outline['#xmlurl']) {
$outline['#title'] ? $item
->set('title', $outline['#title']) : $item
->set('title', $outline['#text']);
$item
->set('categories', $categories)
->set('xmlurl', $outline['#xmlurl'])
->set('htmlurl', $outline['#htmlurl']);
$items[] = $item;
}
if ($outline['outlines']) {
$sub_categories = array_merge($categories, [
$outline['#text'],
]);
$items = array_merge($items, $this
->getItems($outline['outlines'], $sub_categories));
}
}
return $items;
}
public function getMappingSources() {
return [
'feed_title' => [
'label' => $this
->t('Feed: Title of the OPML file'),
'description' => $this
->t('Title of the feed.'),
],
'title' => [
'label' => $this
->t('Title'),
'description' => $this
->t('Title of the feed.'),
'suggestions' => [
'targets' => [
'subject',
'title',
'label',
'name',
],
'types' => [
'field_item:text' => [],
],
],
],
'xmlurl' => [
'label' => $this
->t('URL'),
'description' => $this
->t('URL of the feed.'),
'suggestions' => [
'targets' => [
'url',
],
],
],
'categories' => [
'label' => $this
->t('Categories'),
'description' => $this
->t('The categories of the feed.'),
'suggestions' => [
'targets' => [
'field_tags',
],
'types' => [
'field_item:taxonomy_term_reference' => [],
],
],
],
'htmlurl' => [
'label' => $this
->t('Site URL'),
'description' => $this
->t('The URL of the site that provides the feed.'),
],
];
}
}