@@ -39,7 +39,7 @@ function unity_internal_link_checker_entity_presave(EntityInterface $entity) {
39
39
preg_match('|https://(.*)|', $original_link, $matches2)) {
40
40
// This is an absolute URL does it match one of the domain names specified
41
41
// 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);
43
43
}
44
44
}
45
45
}
@@ -53,33 +53,32 @@ function unity_internal_link_checker_entity_presave(EntityInterface $entity) {
53
53
/**
54
54
* Utility function to convert internal link to relative.
55
55
*/
56
- function _convert_absolute_link (&$body_text, $original_link) {
56
+ function unity_internal_link_checker__convert_absolute_link (&$body_text, $original_link) {
57
57
// Get list of urls to replace
58
58
// (these have been added at
59
59
// /admin/config/unity_internal_link_checker/link_checker_form )
60
60
$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');
62
62
if (strlen($replace_url_list) == 0) {
63
63
\Drupal::messenger()
64
64
->addMessage(t("Please set some URLs at /admin/config/unity_internal_link_checker/link_checker_form"), "error");
65
65
return $body_text;
66
66
}
67
67
$replace_url_list = explode(PHP_EOL, $replace_url_list);
68
+ // Remove empty array items.
69
+ $replace_url_list = array_filter($replace_url_list);
68
70
foreach ($replace_url_list as $replace_url) {
69
71
// Make sure url is 'clean'.
70
72
$replace_url = str_replace(["\n", "\t", "\r"], '', $replace_url);
71
73
// Make sure that there is a trailing slash.
72
74
if (!preg_match('|\/$|', $replace_url)) {
73
75
$replace_url .= '/';
74
76
}
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);
77
77
// Does the start of this link match one of the
78
78
// urls that we are looking for ?
79
79
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);
83
82
}
84
83
}
85
84
return $body_text;
@@ -88,7 +87,7 @@ function _convert_absolute_link(&$body_text, $original_link) {
88
87
/**
89
88
* Utility function to add all domains and extensions for the current site.
90
89
*/
91
- function _urls_to_replace (): string {
90
+ function unity_internal_link_checker__urls_to_replace (): string {
92
91
// Get the host name of the site and remove the domain extension.
93
92
$host = \Drupal::request()->getHost();
94
93
if (preg_match('/\S*?\.(uk|com|net|eu)/', $host, $matches)) {
0 commit comments