I'm trying to get my site to use only relative URLs, like in the question drupal.stackexchange.com/questions/105932
EDIT 3: It seems like Drupal 8.0.3 and beyond will do this by default:
https://www.drupal.org/node/2650374
But I've changed the code for Drupal 8 to this by getting rid of the deprecated function:
function custom_file_url_alter(&$uri) {
global $base_path;
$scheme = file_uri_scheme($uri);
if (!$scheme || $scheme == 'public') {
if (!$scheme) {
$path = $uri;
}
else {
////$wrapper = file_stream_wrapper_get_instance_by_scheme($scheme);
$wrapper = \Drupal::service('stream_wrapper_manager')->getViaScheme($scheme);
$path = $wrapper->getDirectoryPath() . '/' . file_uri_target($uri);
}
// Clean up Windows paths.
$path = str_replace('\\', '/', $path);
$uri = $base_path . ltrim($path, '/');
}
}
The problem is, whenever I add even just an article with an image (or any content type with a file in it) I get an error like this: 'Uncaught PHP Exception InvalidArgumentException: "The URI '/sites/default/files/field/image/h.jpg' is invalid. You must use a valid URI scheme." at /srv/www/example/core/lib/Drupal/Core/Url.php line 301, referer: http://example.com/node/add/article
. I'm also getting this error when visiting a page that links to certain uploaded files.
The documentation says that this hook is called whenever the file_create_url method is called, but I don't know enough about Drupal to understand what scenario this method is being called which is causing Url.php to get mad at me. For what reasons does Drupal call file_create_url, and why are almost all scenarios working for me while some are freaking out about no scheme being used?
Any extra information or guidance about this will be appreciated.
EDIT:
Here are some examples of $uri as it comes into the function (1) and as it leaves (2) .
1: 'core/themes/stable/images/core/icons/5181c6/pencil.svg'
2: '/core/themes/stable/images/core/icons/5181c6/pencil.svg'
1: 'public://css/css_Ys2L0rGQLjkWZQC7f9g5LiLUni8xUQvB1HqJHn3TKK4.css'
2: '/sites/default/files/css/css_Ys2L0rGQLjkWZQC7f9g5LiLUni8xUQvB1HqJHn3TKK4.css'
1: 'public://field/image/h_0.jpg'
2: '/sites/default/files/field/image/h_0.jpg'
The h_0 example is one that happened after I created an article with the h.jpg image, which crashed and prevented creation of the article. I've noticed that when I upload the image using the form, the little ajax thing that happens and shows you the preview of the image doesn't work when I use this file_url_alter code. So, something is trying to call file_create_url when saving the image in the first place, and then, since the image is not properly saved, it can't be added as a field of the node.
Also, I've printed a backtrace, and it seems the crash happens after ThemeManager.php's render function calls template_preprocess_file_link(), which, in file.module, tries to do this:
$variables['link'] = \Drupal::l($link_text, Url::fromUri($url, $options));
...which caused the error I mentioned in the first place.
EDIT 2:
I've made this work by modifying one line of core. I changed the last line of template_preprocess_file_link() in file.module from $variables['link'] = \Drupal::l($link_text, Url::fromUri($url, $options));
to $variables['link'] = $url;
. This makes all URLs relative without any noticeable issues. Does anyone know how to achieve this without hacking core? Or, does anyone know what negative effects doing this might cause?
public://field/image/h.jpg
if that helps