Skip to content

Commit 44fc653

Browse files
authored
Merge pull request #35 from dof-dss/uilc
Ensuring body links aren't changed by the link checker
2 parents 01f241a + 3513133 commit 44fc653

File tree

1 file changed

+8
-9
lines changed

1 file changed

+8
-9
lines changed

unity_internal_link_checker/unity_internal_link_checker.module

+8-9
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ function unity_internal_link_checker_entity_presave(EntityInterface $entity) {
3939
preg_match('|https://(.*)|', $original_link, $matches2)) {
4040
// This is an absolute URL does it match one of the domain names specified
4141
// at /admin/config/unity_internal_link_checker/link_checker_form ?
42-
$body = _convert_absolute_link($body, $original_link);
42+
$body = unity_internal_link_checker__convert_absolute_link($body, $original_link);
4343
}
4444
}
4545
}
@@ -53,33 +53,32 @@ function unity_internal_link_checker_entity_presave(EntityInterface $entity) {
5353
/**
5454
* Utility function to convert internal link to relative.
5555
*/
56-
function _convert_absolute_link(&$body_text, $original_link) {
56+
function unity_internal_link_checker__convert_absolute_link(&$body_text, $original_link) {
5757
// Get list of urls to replace
5858
// (these have been added at
5959
// /admin/config/unity_internal_link_checker/link_checker_form )
6060
$config = \Drupal::config('unity_internal_link_checker.linksettings');
61-
$replace_url_list = _urls_to_replace() . $config->get('site_url_list');
61+
$replace_url_list = unity_internal_link_checker__urls_to_replace() . $config->get('site_url_list');
6262
if (strlen($replace_url_list) == 0) {
6363
\Drupal::messenger()
6464
->addMessage(t("Please set some URLs at /admin/config/unity_internal_link_checker/link_checker_form"), "error");
6565
return $body_text;
6666
}
6767
$replace_url_list = explode(PHP_EOL, $replace_url_list);
68+
// Remove empty array items.
69+
$replace_url_list = array_filter($replace_url_list);
6870
foreach ($replace_url_list as $replace_url) {
6971
// Make sure url is 'clean'.
7072
$replace_url = str_replace(["\n", "\t", "\r"], '', $replace_url);
7173
// Make sure that there is a trailing slash.
7274
if (!preg_match('|\/$|', $replace_url)) {
7375
$replace_url .= '/';
7476
}
75-
// Parse the original link as we need the path from it to replace in the body text.
76-
$original_link_parsed = parse_url($original_link);
7777
// Does the start of this link match one of the
7878
// urls that we are looking for ?
7979
if (str_contains($original_link, $replace_url)) {
80-
// We have a match, change this absolute link
81-
// to a relative link.
82-
$body_text = preg_replace('~' . $original_link . '~', $original_link_parsed['path'], $body_text, '1');
80+
// We have a match, change this absolute link to a relative link. Only target href values.
81+
$body_text = preg_replace('~href\=[\'"]+' . $replace_url . '~', 'href="/', $body_text, 1);
8382
}
8483
}
8584
return $body_text;
@@ -88,7 +87,7 @@ function _convert_absolute_link(&$body_text, $original_link) {
8887
/**
8988
* Utility function to add all domains and extensions for the current site.
9089
*/
91-
function _urls_to_replace(): string {
90+
function unity_internal_link_checker__urls_to_replace(): string {
9291
// Get the host name of the site and remove the domain extension.
9392
$host = \Drupal::request()->getHost();
9493
if (preg_match('/\S*?\.(uk|com|net|eu)/', $host, $matches)) {

0 commit comments

Comments
 (0)