Problem/Motivation
when I changed the file url by hook_file_url_alter
/**
* Implements hook_file_url_alter().
*/
function astellas_domain_regional_file_url_alter(&$uri) {
$active_domain = \Drupal::config('astellas_domain_regional.form_settings');
$domain_suffix = $active_domain->get('text_domain');
$base_path = PublicStream::basePath();
if (strpos($uri, 'private://') === 0 && !empty($domain_suffix)) {
$uri = str_replace('private://', $domain_suffix . '/system/files/', $uri);
}
if (strpos($uri, 'public://') === 0 && !empty($domain_suffix)) {
$uri = str_replace('public://', $domain_suffix . '/' . $base_path . '/', $uri);
}
if (!preg_match("/^(http|https):\/\//i", $uri) && !preg_match("/^([a-zA-Z]{2})\//", $uri) &&
!preg_match("/^\/([a-zA-Z]{2})\//", $uri) && !empty($domain_suffix) && !str_starts_with($uri, '//')) {
$uri = $domain_suffix . '/' . $uri;
}
}
the file url will be encode in core/lib/Drupal/Core/File/FileUrlGenerator.php 179 line.
$options = UrlHelper::parse($uri);
return Url::fromUri('base:' . UrlHelper::encodePath($options['path']), $options);
if the file path include space, uri will be encode.
Proposed resolution
remove the encodePath function.