-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: add target and rel attributes to external links to open in new t…
…ab for links inside fields using the markdown_editor (ckeditor 5) format Refs: RW-812
- Loading branch information
Showing
2 changed files
with
226 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
142 changes: 142 additions & 0 deletions
142
html/modules/custom/reliefweb_utility/src/Plugin/Filter/ExternalLinkFilter.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,142 @@ | ||
<?php | ||
|
||
namespace Drupal\reliefweb_utility\Plugin\Filter; | ||
|
||
use Drupal\Component\Render\MarkupInterface; | ||
use Drupal\Core\Plugin\ContainerFactoryPluginInterface; | ||
use Drupal\filter\FilterProcessResult; | ||
use Drupal\filter\Plugin\FilterBase; | ||
use Symfony\Component\DependencyInjection\ContainerInterface; | ||
use Symfony\Component\HttpFoundation\RequestStack; | ||
|
||
/** | ||
* Provides a filter to add attributes to external links to open in a new tab. | ||
* | ||
* @Filter( | ||
* id = "reliefweb_external_link_filter", | ||
* title = @Translation("Open external Links in new tab."), | ||
* description = @Translation("Add target and rel attributes to external links so they open in a new tab."), | ||
* type = Drupal\filter\Plugin\FilterInterface::TYPE_TRANSFORM_IRREVERSIBLE, | ||
* ) | ||
*/ | ||
class ExternalLinkFilter extends FilterBase implements ContainerFactoryPluginInterface { | ||
|
||
/** | ||
* Current request. | ||
* | ||
* @var \Symfony\Component\HttpFoundation\RequestStack | ||
*/ | ||
protected $requestStack; | ||
|
||
/** | ||
* Internal host pattern. | ||
* | ||
* @var string | ||
*/ | ||
protected $internalHostPattern; | ||
|
||
/** | ||
* Constructs a markdown filter plugin. | ||
* | ||
* @param array $configuration | ||
* A configuration array containing information about the plugin instance. | ||
* @param string $plugin_id | ||
* The plugin_id for the plugin instance. | ||
* @param mixed $plugin_definition | ||
* The plugin implementation definition. | ||
* @param \Symfony\Component\HttpFoundation\RequestStack $request_stack | ||
* The request stack. | ||
*/ | ||
public function __construct(array $configuration, $plugin_id, $plugin_definition, RequestStack $request_stack) { | ||
parent::__construct($configuration, $plugin_id, $plugin_definition); | ||
$this->requestStack = $request_stack; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) { | ||
return new static( | ||
$configuration, | ||
$plugin_id, | ||
$plugin_definition, | ||
$container->get('request_stack') | ||
); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function process($text, $langcode) { | ||
if (is_string($text) || $text instanceof MarkupInterface) { | ||
// Adding this meta tag is necessary to tell \DOMDocument we are dealing | ||
// with UTF-8 encoded html. | ||
$flags = LIBXML_NONET | LIBXML_NOBLANKS | LIBXML_NOERROR | LIBXML_NOWARNING; | ||
$meta = '<meta http-equiv="Content-Type" content="text/html; charset=utf-8">'; | ||
$prefix = '<!DOCTYPE html><html><head>' . $meta . '</head><body>'; | ||
$suffix = '</body></html>'; | ||
$dom = new \DOMDocument(); | ||
$dom->loadHTML($prefix . $text . $suffix, $flags); | ||
|
||
// Try to get the body. | ||
$links = $dom->getElementsByTagName('a'); | ||
foreach ($links as $link) { | ||
$this->handleLink($link); | ||
} | ||
|
||
$html = $dom->saveHTML(); | ||
|
||
// Search for the body tag and return its content. | ||
$start = mb_strpos($html, '<body>'); | ||
$end = mb_strrpos($html, '</body>'); | ||
if ($start !== FALSE && $end !== FALSE) { | ||
$start += 6; | ||
$text = trim(mb_substr($html, $start, $end - $start)); | ||
} | ||
} | ||
return new FilterProcessResult($text); | ||
} | ||
|
||
/** | ||
* Check if a URL is an ReliefWeb URL. | ||
* | ||
* @param string $url | ||
* URL to check. | ||
* | ||
* @return bool | ||
* TRUE if the URL is internal. | ||
*/ | ||
protected function isInternalUrl($url) { | ||
if (empty($url)) { | ||
return TRUE; | ||
} | ||
|
||
if (!isset($this->internalHostPattern)) { | ||
$internal_hosts = [ | ||
preg_quote($this->requestStack->getCurrentRequest()->getHost()), | ||
preg_quote('reliefweb.int'), | ||
]; | ||
|
||
$this->internalHostPattern = '#^https?://(' . implode('|', $internal_hosts) . ')(/|$)#'; | ||
} | ||
|
||
return preg_match('#^https?://#', $url) !== 1 || | ||
preg_match($this->internalHostPattern, $url) === 1; | ||
} | ||
|
||
/** | ||
* Add the target and rel attributes to external links to open in a new tab. | ||
* | ||
* @param \DOMNode $node | ||
* Link node. | ||
*/ | ||
protected function handleLink(\DOMNode $node) { | ||
$url = $node->getAttribute('href'); | ||
|
||
if (!$this->isInternalUrl($url)) { | ||
$node->setAttribute('target', '_blank'); | ||
$node->setAttribute('rel', 'noreferrer noopener'); | ||
} | ||
} | ||
|
||
} |