Wrong url generated for file fields

Created on 22 March 2022, over 3 years ago
Updated 12 July 2025, 1 day ago

Problem/Motivation

Updating an existing site, the urls generated for the file fields generates wrong.
The url looks like $your_currentsite.com.$actuallFileUrl
your_currentsite.comhttps%3A//your_currentsite.com/asset-url

Steps to reproduce

1.Create a file field with core version 9.2 or before.
2. Add a field type file
3. Update the core to 9.3 or above

Proposed resolution

Adding patch that reverts the following
code

 /** @var \Drupal\Core\File\FileUrlGeneratorInterface $file_url_generator */
  $file_url_generator = \Drupal::service('file_url_generator');
  $url = $file_url_generator->generate($file->getFileUri());

to thee initial one

 $url = $file->createFileUrl(FALSE);
  $variables['#cache']['contexts'][] = 'url.site';
🐛 Bug report
Status

Closed: outdated

Version

11.0 🔥

Component

file system

Created by

🇭🇷Croatia marijan gudelj

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

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • 🇮🇳India mohit_aghera Rajkot

    I tried to reproduce that by checking out 9.2.21 and then upgrading to 9.3.22
    I added a file field and performed upgrade by doing code change.
    I was not able to locate specific issues in core.

    Apart from that I'm somewhat uncertain about what part of core is breaking.
    The patches seem to be pointing about template_preprocess_file_link()
    I checked the code and it looks good on the latest 11.x

    Moreover the proposed code in patches is calling createFileUrl() on file entity.
    Under the hood, it is doing the same thing.

    public function createFileUrl($relative = TRUE) {
        /** @var \Drupal\Core\File\FileUrlGeneratorInterface $file_url_generator */
        $file_url_generator = \Drupal::service('file_url_generator');
        return $relative ? $file_url_generator->generateString($this->getFileUri()) : $file_url_generator->generateAbsoluteString($this->getFileUri());
      }
    

    I believe we can close the issue.

    Please reopen again if you notice the issue is happening again.
    I'm happy to come up with the patch and fixes.

Production build 0.71.5 2024