class OpmlParser in Feeds 8.3
Defines an OPML feed parser.
Plugin annotation
@FeedsParser(
id = "opml",
title = @Translation("OPML"),
description = @Translation("Parse OPML files.")
)
Hierarchy
- class \Drupal\Component\Plugin\PluginBase implements DerivativeInspectionInterface, PluginInspectionInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
- class \Drupal\feeds\Plugin\Type\PluginBase implements FeedsPluginInterface uses DependencyTrait
- class \Drupal\feeds\Feeds\Parser\OpmlParser implements ParserInterface
- class \Drupal\feeds\Plugin\Type\PluginBase implements FeedsPluginInterface uses DependencyTrait
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
Expanded class hierarchy of OpmlParser
1 file declares its use of OpmlParser
- OpmlParserTest.php in tests/
src/ Unit/ Feeds/ Parser/ OpmlParserTest.php
File
- src/
Feeds/ Parser/ OpmlParser.php, line 24
Namespace
Drupal\feeds\Feeds\ParserView source
class OpmlParser extends PluginBase implements ParserInterface {
/**
* {@inheritdoc}
*/
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;
}
/**
* Returns a flattened array of feed items.
*
* @param array $outlines
* A nested array of outlines.
* @param array $categories
* For internal use only.
*
* @return array
* The flattened list of feed items.
*/
protected function getItems(array $outlines, array $categories = []) {
$items = [];
foreach ($outlines as $outline) {
// PHPunit is being weird about our array appending.
// @codeCoverageIgnoreStart
$outline += [
'#title' => '',
'#text' => '',
'#xmlurl' => '',
'#htmlurl' => '',
'outlines' => [],
];
// @codeCoverageIgnoreEnd
$item = new OpmlItem();
// Assume it is an actual feed if the URL is set.
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;
}
// Get sub elements.
if ($outline['outlines']) {
$sub_categories = array_merge($categories, [
$outline['#text'],
]);
$items = array_merge($items, $this
->getItems($outline['outlines'], $sub_categories));
}
}
return $items;
}
/**
* {@inheritdoc}
*/
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.'),
],
];
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
DependencySerializationTrait:: |
protected | property | An array of entity type IDs keyed by the property name of their storages. | |
DependencySerializationTrait:: |
protected | property | An array of service IDs keyed by property name used for serialization. | |
DependencySerializationTrait:: |
public | function | 1 | |
DependencySerializationTrait:: |
public | function | 2 | |
DependencyTrait:: |
protected | property | The object's dependencies. | |
DependencyTrait:: |
protected | function | Adds multiple dependencies. | |
DependencyTrait:: |
protected | function | Adds a dependency. | |
MessengerTrait:: |
protected | property | The messenger. | 29 |
MessengerTrait:: |
public | function | Gets the messenger. | 29 |
MessengerTrait:: |
public | function | Sets the messenger. | |
OpmlParser:: |
protected | function | Returns a flattened array of feed items. | |
OpmlParser:: |
public | function |
Declare the possible mapping sources that this parser produces. Overrides ParserInterface:: |
|
OpmlParser:: |
public | function |
Parses content returned by fetcher. Overrides ParserInterface:: |
|
PluginBase:: |
protected | property | Configuration information passed into the plugin. | 1 |
PluginBase:: |
protected | property | The importer this plugin is working for. | |
PluginBase:: |
protected | property | The link generator. | |
PluginBase:: |
protected | property | The plugin implementation definition. | 1 |
PluginBase:: |
protected | property | The plugin_id. | |
PluginBase:: |
protected | property | The url generator. | |
PluginBase:: |
public | function |
Calculates dependencies for the configured plugin. Overrides DependentPluginInterface:: |
2 |
PluginBase:: |
private | function | Returns the service container. | |
PluginBase:: |
public | function |
Gets default configuration for this plugin. Overrides ConfigurableInterface:: |
7 |
PluginBase:: |
public | function |
Returns default feed configuration. Overrides FeedsPluginInterface:: |
3 |
PluginBase:: |
constant | A string which is used to separate base plugin IDs from the derivative ID. | ||
PluginBase:: |
public | function |
Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets this plugin's configuration. Overrides ConfigurableInterface:: |
|
PluginBase:: |
public | function |
Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the definition of the plugin implementation. Overrides PluginInspectionInterface:: |
3 |
PluginBase:: |
public | function |
Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface:: |
|
PluginBase:: |
public | function | Determines if the plugin is configurable. | |
PluginBase:: |
protected | function | Renders a link to a route given a route name and its parameters. | |
PluginBase:: |
protected | function | Returns the link generator service. | |
PluginBase:: |
public | function | A feed is being deleted. | 3 |
PluginBase:: |
public | function | A feed is being saved. | |
PluginBase:: |
public | function | The feed type is being deleted. | 1 |
PluginBase:: |
public | function | The feed type is being saved. | 1 |
PluginBase:: |
public | function |
Returns the type of plugin. Overrides FeedsPluginInterface:: |
|
PluginBase:: |
public | function |
Sets the configuration for this plugin instance. Overrides ConfigurableInterface:: |
1 |
PluginBase:: |
protected | function | Generates a URL or path for a specific route based on the given parameters. | |
PluginBase:: |
protected | function | Returns the URL generator service. | |
PluginBase:: |
public | function |
Constructs a PluginBase object. Overrides PluginBase:: |
4 |
StringTranslationTrait:: |
protected | property | The string translation service. | 1 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. |