Problem/Motivation

After #2669074: Convert file_create_url() & file_url_transform_relative() to service, deprecate it fix, FileUriFormatter is not returning absolute URL.
As per the change record which states:

  1. The method generateString() which creates a root-relative web-accessible URL string
  2. The method generateAbsoluteString() which creates an absolute web-accessible URL string;

file_create_url() is supposed to be changed to \Drupal::service('file_url_generator')->generateAbsoluteString($uri)

Instead of that, it is updated with $this->fileUrlGenerator->generateString($uri)

That causes the regression.

Steps to reproduce

Check the FileUriFormatter before #2669074: Convert file_create_url() & file_url_transform_relative() to service, deprecate it and after the fix.

Proposed resolution

Fix the incorrect change and replace with correct method.

Remaining tasks

N/A

User interface changes

N/A

Introduced terminology

N/A

API changes

N/A

Data model changes

N/A

Release notes snippet

N/A

Original report by @joakland

Following upgrade to Drupal 9.3.0, checking the "Display the file download URI" for a "File: URI" field in Views no longer displays an absolute path; now it displays a relative path.

Issue fork drupal-3253897

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

joakland created an issue. See original summary.

lendude’s picture

Title: "Display the file download URI" on related file entity shows relative path » Regression: "Display the file download URI" on related file entity shows relative path
Component: views.module » file.module
Issue tags: -views, -files

Quick little git-blame points to #2669074: Convert file_create_url() & file_url_transform_relative() to service, deprecate it, so that would not make this due to a change in Views but on the field formatter.

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.15 was released on June 1st, 2022 and is the final full bugfix release for the Drupal 9.3.x series. Drupal 9.3.x will not receive any further development aside from security fixes. Drupal 9 bug reports should be targeted for the 9.4.x-dev branch from now on, and new development or disruptive changes should be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.9 was released on December 7, 2022 and is the final full bugfix release for the Drupal 9.4.x series. Drupal 9.4.x will not receive any further development aside from security fixes. Drupal 9 bug reports should be targeted for the 9.5.x-dev branch from now on, and new development or disruptive changes should be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.5.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

mohit_aghera made their first commit to this issue’s fork.

mohit_aghera’s picture

This certainly seems a regression because of issue #2669074: Convert file_create_url() & file_url_transform_relative() to service, deprecate it

As per the change record file_create_url()<code> is supposed to be moved to <code>\Drupal::service('file_url_generator')->generateAbsoluteString($uri)

Instead of that, it is updated with $this->fileUrlGenerator->generateString($uri)

That sounds the root cause of the regression.
Probably this wasn't caught becuase we don't have test coverage for this one.

I've raised a PR with the fix and working on test coverage.

mohit_aghera’s picture

Status: Active » Needs review
smustgrave’s picture

Status: Needs review » Needs work
Issue tags: +Needs issue summary update

Can the issue summary be updated to include the issue this was added? May have been done on purpose. 4 years makes me wonder if it was a regression

mohit_aghera’s picture

Issue summary: View changes
Status: Needs work » Needs review
Issue tags: -Needs issue summary update

I checked the gigantic thread of the issue #2669074: Convert file_create_url() & file_url_transform_relative() to service, deprecate it

The changes approximately starts from comment 83 https://www.drupal.org/project/drupal/issues/2669074#comment-13003933

I don't see any specific discussion of switching generateString instead of generateAbsoluteString to FileUriFormatter.

I believe this issue might not have caught till date because there might be very few sites who are using URI formatter.

I've updated issue summary as well. Happy to close if required.

mohit_aghera’s picture

Issue summary: View changes
smustgrave’s picture

Status: Needs review » Reviewed & tested by the community
Issue tags: +Needs Review Queue Initiative, +Bug Smash Initiative

Apologize I've had this tab open for like 3 days and keep getting distracted

Summary looks good now thanks for that!

Ran the test-only feature and got

1) Drupal\Tests\file\Kernel\Formatter\FileEntityFormatterTest::testFormatterFileUri
Failed asserting that two strings are equal.
--- Expected
+++ Actual
@@ @@
-'http://localhost/vfs://root/sites/simpletest/84724206/files/file.png'
+'/vfs://root/sites/simpletest/84724206/files/file.png'
/builds/issue/drupal-3253897/core/modules/file/tests/src/Kernel/Formatter/FileEntityFormatterTest.php:109
FAILURES!
Tests: 6, Assertions: 31, Failures: 1.

Wasn't 100% how to manually test but use of generateAbsoluteString() per the CR

oily made their first commit to this issue’s fork.

cassioalmeida’s picture

Hi everyone,

I created a patch from MR and applied it to 10.5.1. Worked perfectly!

oily’s picture

Test-only test output:

Time: 00:15.009, Memory: 8.00 MB
There was 1 failure:
1) Drupal\Tests\file\Kernel\Formatter\FileEntityFormatterTest::testFormatterFileUri
Failed asserting that two strings are equal.
oily’s picture

Issue summary: View changes
oily’s picture

xjm’s picture

For #16, it's actually usually preferred to document the output of the failing test for the test-only in the job in a code tag, like so:

// Failed job output here

N methods failed Y asserts completed in blah blah

It's not sufficient that it fails; it needs to fail in the right way and then we document that.

But that has already been done for this issue, so there is no need to repeat it after RTBC. 🙂

I guess you were trying to say your rebase had the same test-only fail as in the previous comment?

oily’s picture

RE: #19

I guess you were trying to say your rebase had the same test-only fail as in the previous comment?

Actually, i think that makes sense. Edited #16. When I read #13 for some reason I got the impression @smustgrave was reporting a problem with the test.

oily’s picture

Issue summary: View changes
oily’s picture

Updated IS.

  • catch committed 8207a2ff on 10.6.x
    Issue #3253897 by mohit_aghera, oily, joakland, smustgrave, lendude:...

  • catch committed 8187a716 on 11.2.x
    Issue #3253897 by mohit_aghera, oily, joakland, smustgrave, lendude:...

  • catch committed fe2f9258 on 11.x
    Issue #3253897 by mohit_aghera, oily, joakland, smustgrave, lendude:...
catch’s picture

Version: 11.x-dev » 10.6.x-dev
Status: Reviewed & tested by the community » Fixed

Committed/pushed to 11.x, cherry-picked to 11.2.x and 10.6.x thanks!

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.