View source
<?php
namespace Drupal\rel_to_abs\Plugin\Filter;
use Drupal\filter\FilterProcessResult;
use Drupal\filter\Plugin\FilterBase;
class RelToAbs extends FilterBase {
public function process($text, $langcode) {
$base_url = \Drupal::url('<front>', array(), array(
'absolute' => TRUE,
'language' => \Drupal::getContainer()
->get('language_manager')
->getLanguage($langcode),
));
$text = $this
->absoluteUrl($text, $base_url);
return new FilterProcessResult($text);
}
public function absoluteUrl($txt, $base_url) {
$needles = array(
'href="',
'src="',
'background="',
);
$new_txt = '';
if (substr($base_url, -1) != '/') {
$base_url .= '/';
}
$new_base_url = $base_url;
$sub_dir = FALSE;
$parts = parse_url($new_base_url);
if ($parts['path'] !== '/') {
$sub_dir = $parts['path'];
}
foreach ($needles as $needle) {
while ($pos = strpos($txt, $needle)) {
$pos += strlen($needle);
if (substr($txt, $pos, 7) != 'http://' && substr($txt, $pos, 8) != 'https://' && substr($txt, $pos, 6) != 'ftp://' && substr($txt, $pos, 7) != 'mailto:' && substr($txt, $pos, 2) != '//' && substr($txt, $pos, 1) != '#' && substr($txt, $pos, 4) != 'tel:') {
$new_txt .= substr($txt, 0, $pos) . $new_base_url;
}
else {
$new_txt .= substr($txt, 0, $pos);
}
$txt = substr($txt, $pos);
if ($sub_dir) {
if (substr($txt, 0, strlen($sub_dir)) == $sub_dir) {
$txt = substr($txt, strlen($sub_dir));
}
}
}
$txt = $new_txt . $txt;
$new_txt = '';
}
return $txt;
}
}