RssResponseRelativeUrlFilter.php in Drupal 8
File
core/lib/Drupal/Core/EventSubscriber/RssResponseRelativeUrlFilter.php
View source
<?php
namespace Drupal\Core\EventSubscriber;
use Drupal\Component\Utility\Html;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
use Symfony\Component\HttpKernel\KernelEvents;
class RssResponseRelativeUrlFilter implements EventSubscriberInterface {
public function onResponse(FilterResponseEvent $event) {
if (stripos($event
->getResponse()->headers
->get('Content-Type'), 'application/rss+xml') === FALSE) {
return;
}
$response = $event
->getResponse();
$response
->setContent($this
->transformRootRelativeUrlsToAbsolute($response
->getContent(), $event
->getRequest()));
}
protected function transformRootRelativeUrlsToAbsolute($rss_markup, Request $request) {
$rss_dom = new \DOMDocument();
$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;
}
foreach ($rss_dom
->getElementsByTagName('description') as $node) {
$html_markup = $node->nodeValue;
if (!empty($html_markup)) {
$node->nodeValue = Html::transformRootRelativeUrlsToAbsolute($html_markup, $request
->getSchemeAndHttpHost());
}
}
return $rss_dom
->saveXML();
}
public static function getSubscribedEvents() {
$events[KernelEvents::RESPONSE][] = [
'onResponse',
-512,
];
return $events;
}
}