class RssResponseRelativeUrlFilter
Same name in other branches
- 9 core/lib/Drupal/Core/EventSubscriber/RssResponseRelativeUrlFilter.php \Drupal\Core\EventSubscriber\RssResponseRelativeUrlFilter
- 8.9.x core/lib/Drupal/Core/EventSubscriber/RssResponseRelativeUrlFilter.php \Drupal\Core\EventSubscriber\RssResponseRelativeUrlFilter
- 10 core/lib/Drupal/Core/EventSubscriber/RssResponseRelativeUrlFilter.php \Drupal\Core\EventSubscriber\RssResponseRelativeUrlFilter
Subscribes to filter RSS responses, to make relative URIs absolute.
Hierarchy
- class \Drupal\Core\EventSubscriber\RssResponseRelativeUrlFilter implements \Symfony\Component\EventDispatcher\EventSubscriberInterface
Expanded class hierarchy of RssResponseRelativeUrlFilter
1 file declares its use of RssResponseRelativeUrlFilter
- RssResponseRelativeUrlFilterTest.php in core/
tests/ Drupal/ Tests/ Core/ EventSubscriber/ RssResponseRelativeUrlFilterTest.php
File
-
core/
lib/ Drupal/ Core/ EventSubscriber/ RssResponseRelativeUrlFilter.php, line 14
Namespace
Drupal\Core\EventSubscriberView source
class RssResponseRelativeUrlFilter implements EventSubscriberInterface {
/**
* Converts relative URLs to absolute URLs.
*
* @param \Symfony\Component\HttpKernel\Event\ResponseEvent $event
* The response event.
*/
public function onResponse(ResponseEvent $event) {
// Only care about RSS responses.
if (stripos($event->getResponse()->headers
->get('Content-Type', ''), 'application/rss+xml') === FALSE) {
return;
}
$response = $event->getResponse();
$response->setContent($this->transformRootRelativeUrlsToAbsolute($response->getContent(), $event->getRequest()));
}
/**
* Converts all root-relative URLs to absolute URLs in RSS markup.
*
* Does not change any existing protocol-relative or absolute URLs.
*
* @param string $rss_markup
* The RSS markup to update.
* @param \Symfony\Component\HttpFoundation\Request $request
* The current request.
*
* @return string
* The updated RSS markup.
*/
protected function transformRootRelativeUrlsToAbsolute($rss_markup, Request $request) {
$rss_dom = new \DOMDocument();
// Load the RSS, if there are parsing errors, abort and return the unchanged
// markup.
$previous_value = libxml_use_internal_errors(TRUE);
$rss_dom->loadXML($rss_markup);
$errors = libxml_get_errors();
libxml_use_internal_errors($previous_value);
if ($errors) {
return $rss_markup;
}
// Invoke Html::transformRootRelativeUrlsToAbsolute() on all HTML content
// embedded in this RSS feed.
foreach ($rss_dom->getElementsByTagName('item') as $item) {
foreach ($item->getElementsByTagName('description') as $node) {
$html_markup = $node->nodeValue;
if (!empty($html_markup)) {
$node->replaceChild($rss_dom->createTextNode(Html::transformRootRelativeUrlsToAbsolute($html_markup, $request->getSchemeAndHttpHost())), $node->firstChild);
}
}
}
return $rss_dom->saveXML();
}
/**
* {@inheritdoc}
*/
public static function getSubscribedEvents() : array {
// Should run after any other response subscriber that modifies the markup.
// Only the CDATA wrapper should run after this filter.
// @see \Drupal\Core\EventSubscriber\ActiveLinkResponseFilter
$events[KernelEvents::RESPONSE][] = [
'onResponse',
-512,
];
return $events;
}
}
Members
Title Sort descending | Modifiers | Object type | Summary |
---|---|---|---|
RssResponseRelativeUrlFilter::getSubscribedEvents | public static | function | |
RssResponseRelativeUrlFilter::onResponse | public | function | Converts relative URLs to absolute URLs. |
RssResponseRelativeUrlFilter::transformRootRelativeUrlsToAbsolute | protected | function | Converts all root-relative URLs to absolute URLs in RSS markup. |
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.