Skip to content
Snippets Groups Projects
Commit 0853e716 authored by Bryan Sharpe's avatar Bryan Sharpe Committed by Robert Phillips
Browse files

Issue #3302931: Don't override existing attribute.

parent 8c653b39
No related branches found
No related tags found
1 merge request!1Issue #3302931: Don't override existing attribute.
......@@ -52,6 +52,19 @@ class TargetAttributesFilter extends FilterBase implements ContainerFactoryPlugi
);
}
/**
* {@inheritdoc}
*/
public function setConfiguration(array $configuration) {
parent::setConfiguration($configuration);
$this->settings = $this->settings + [
'filter_target_attribute' => '_blank',
'filter_target_method' => 'all',
'filter_target_replace' => 1,
];
return $this;
}
/**
* {@inheritdoc}
*/
......@@ -79,6 +92,12 @@ class TargetAttributesFilter extends FilterBase implements ContainerFactoryPlugi
'#default_value' => $this->settings['filter_target_method'],
'#required' => TRUE,
];
$form['filter_target_replace'] = [
'#type' => 'checkbox',
'#title' => $this->t('Replace target attribute'),
'#description' => $this->t('If checked, any existing target attributes value is replaced.'),
'#default_value' => $this->settings['filter_target_replace'],
];
return $form;
}
......@@ -89,6 +108,10 @@ class TargetAttributesFilter extends FilterBase implements ContainerFactoryPlugi
$dom = Html::load($text);
$internalHost = $this->requestStack->getMainRequest()->getHttpHost();
foreach ($dom->getElementsByTagName('a') as $link) {
/** @var \DOMElement $link */
if (!$this->settings['filter_target_replace'] && $link->hasAttribute('target')) {
continue;
}
if ($href = trim($link->getAttribute('href'))) {
$url = parse_url($href);
$host = preg_replace('/^www\./i', '', $url['host'] ?? $internalHost);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment