Problem/Motivation

Make images work out of the box. Same for for file downloads.

Steps to reproduce

* Example with image field
* Example with media entity
* Example with file reference

Proposed resolution

Variant A:
* Have images be served with absolute URLs by default.
* Allow disabling changing the URLs
Variant B:
* Add some proxy to frontend to make it work out of the box. But that would only work with SSR. Document solution for SSG.

Remaining tasks

* Test default processors, whether works should be done there.
* Make the URLs absolute.

Related information

* #2754273: Image styles fails with absolute path in $settings['file_public_path'] - the setting is not respected with absolute URLs
* hook_file_url_alter() seems to be not a solution, Drupal still outputs relative URLs
* seems we need to decorate FileURLGenerator service like CDN module does: https://git.drupalcode.org/project/cdn/-/blob/4.x/src/File/FileUrlGenera...

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

fago created an issue. See original summary.

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

useernamee’s picture

So I had file_base_url service injected into the decorated file url generated but then I've noticed that it already has absolute file url generation method and I hope this one will suffice.

useernamee’s picture

Assigned: Unassigned » fago
Status: Active » Needs review

fago’s picture

Status: Needs review » Needs work

reviewed the MR and added a couple of remarks. Main part is we should make sure to only do this when serving /ce-api responses:

> Serving files and images using absolute URLs keeps the file URLs working in a decoupled frontend. When disabled, care must be taken to resolve URLs otherwise.

fago’s picture

> Main part is we should make sure to only do this when serving /ce-api responses:

This can be checked by getting current request and doing:

$is_api_response = $processed_request->attributes->get('lupus_ce_renderer') || $processed_request->getRequestFormat() == 'custom_elements';

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

fago’s picture

thx, changes look all good now, but I don't see it working. Does it work for you?

"content": "\n <div class=\"wide-image primary-image field field--name-field-image field--type-image field--label-hidden field__item\"> <img loading=\"lazy\" src=\"/sites/default/files/styles/wide/public/2023-03/continental.jpg?itok=_N-3d5Sh\" width=\"676\" height=\"489\" alt=\"dsafasdf\" />\n\n\n</div>\n "

Maybe the API-response is not detected? your way of doing it seems ok, but maybe it's not ? else use;:
$is_api_response = $processed_request->attributes->get('lupus_ce_renderer') || $processed_request->getRequestFormat() == 'custom_elements';

petar_basic’s picture

I've tried with $is_api_response = $processed_request->attributes->get('lupus_ce_renderer') || $processed_request->getRequestFormat() == 'custom_elements';
but it still does not work.
Needs more debugging to see why it is not set.

fago’s picture

we must override both generateString() and generate() to return the absolute URL, see the working example of CDN module's \Drupal\cdn\File\FileUrlGenerator - then it should start working.

petar_basic’s picture

I've overrided the generate method now as well. Also moved the common code into the separate helper function.

Tested locally and on drupal pod, works well for me now.

"content":"\n <div class=\"wide-image primary-image field field--name-field-image field--type-image field--label-hidden field__item\" <img loading=\u0022lazy\u0022 src=\"https:\/\/8080-shaal-drupalpod-k6ot9a4erhh.ws-eu89b.gitpod.io\/sites\/default\/files\/styles\/wide\/public\/2023-03\/Screenshot%20from%202022-12-06%2009-59-05.png?itok=C9VjS8XY\" width=\u0022589\u0022 height=\"583\" alt=\"test\" ></div> "},

  • fago committed b15c7a5f on 1.x
    Issue #3343956: Add out of the box support for images
    
fago’s picture

Status: Needs work » Fixed

Perfect, thank you!

Status: Fixed » Closed (fixed)

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