- π¬π§United Kingdom globexplorer
Drupal should allow absolute links. At the moment I tracked the problem down to the RssFields row plugin located at
core/modules/views/src/Plugin/views/rowRelative urls are expected, because inside the row plugin, the relative urls will be transformed to aboslute ones. But of course limited to the drupal backend host.
This function makes it impossible to use absolute URLs:
/** * Convert a rendered URL string to an absolute URL. * * @param string $url_string * The rendered field value ready for display in a normal view. * * @return string * A string with an absolute URL. */ protected function getAbsoluteUrl($url_string) { // If the given URL already starts with a leading slash, it's been processed // and we need to simply make it an absolute path by prepending the host. if (strpos($url_string, '/') === 0) { $host = \Drupal::request()->getSchemeAndHttpHost(); // @todo Views should expect and store a leading /. // @see https://www.drupal.org/node/2423913 return $host . $url_string; } // Otherwise, this is an unprocessed path (e.g. node/123) and we need to run // it through a Url object to allow outbound path processors to run (path // aliases, language prefixes, etc). else { return Url::fromUserInput('/' . $url_string)->setAbsolute()->toString(); } }
I have created a patch for the 9.5.x release of drupal.
- πΊπΈUnited States Kasey_MK
Thanks @globexplorer! The patch in #14 didn't quite work for me (I'm sometimes using absolute URLs to external sites from a link field instead of linking to the stub pages on the current domain, where 'Use absolute link' would apply), but you pointed me in the direction of a change that does seem to do the trick for how I'm using it.
Instead of using UrlHelper to decide whether or not to send the $url_string to the getAbsoluteUrl function, I used it within that function itself - if the $url_string is already valid (absolute) then it can be sent right back and needs no further processing:
@@ -207,9 +208,13 @@ public function getField($index, $field_id) { * A string with an absolute URL. */ protected function getAbsoluteUrl($url_string) { + // If the given URL is already valid, allow it. + if (UrlHelper::isValid($url_string, TRUE)) { + return $url_string; + } // If the given URL already starts with a leading slash, it's been processed // and we need to simply make it an absolute path by prepending the host. - if (str_starts_with($url_string, '/')) { + elseif (str_starts_with($url_string, '/')) {
Attached are two patches: one that applies against 10.2, which I'm currently using, and one against 11.x.
- πΊπΈUnited States Kasey_MK
I think this may be a duplicate issue: Views RSS row plugin incorrectly assumes links are relative π Views RSS row plugin incorrectly assumes links are relative Needs work