class MyFeed in Feeds 8.3
Alters the parsed result for the feed type 'my_feed'.
Hierarchy
- class \Drupal\feeds_test_extra_sources\EventSubscriber\MyFeed implements \Symfony\Component\EventDispatcher\EventSubscriberInterface
Expanded class hierarchy of MyFeed
1 string reference to 'MyFeed'
- feeds_test_extra_sources.services.yml in tests/
modules/ feeds_test_extra_sources/ feeds_test_extra_sources.services.yml - tests/modules/feeds_test_extra_sources/feeds_test_extra_sources.services.yml
1 service uses MyFeed
- feeds_test_extra_sources.my_feed in tests/
modules/ feeds_test_extra_sources/ feeds_test_extra_sources.services.yml - Drupal\feeds_test_extra_sources\EventSubscriber\MyFeed
File
- tests/
modules/ feeds_test_extra_sources/ src/ EventSubscriber/ MyFeed.php, line 12
Namespace
Drupal\feeds_test_extra_sources\EventSubscriberView source
class MyFeed implements EventSubscriberInterface {
/**
* {@inheritdoc}
*/
public static function getSubscribedEvents() {
return [
FeedsEvents::PARSE => [
[
'afterParse',
FeedsEvents::AFTER,
],
],
];
}
/**
* Acts on parser result.
*/
public function afterParse(ParseEvent $event) {
if ($event
->getFeed()
->getType()
->id() != 'my_feed') {
// Not interested in this feed. Abort.
return;
}
/** @var \Drupal\feeds\Feeds\Item\ItemInterface $item */
foreach ($event
->getParserResult() as $item) {
$title = $item
->get('title');
$slogan = $item
->get('site:slogan');
// Set title to lowercase.
$item
->set('title', strtolower($title));
// Get first word from title.
$word = strtok($title, ' ');
// Strip all chars except letters and dashes.
$word = preg_replace('/[^a-zA-Z\\-]/', '', $word);
// And alter slogan.
$item
->set('site:slogan', str_replace('It', $word, $slogan));
}
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
MyFeed:: |
public | function | Acts on parser result. | |
MyFeed:: |
public static | function | Returns an array of event names this subscriber wants to listen to. |