When use hook_file_url_alter to change file url, the file path will be encode.

Created on 31 July 2023, about 1 year ago

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.

πŸ› Bug report
Status

Postponed: needs info

Version

10.1 ✨

Component
File systemΒ  β†’

Last updated 1 day ago

Created by

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Production build 0.71.5 2024