Add out-of-the-box support for images and files

Created on 23 February 2023, over 1 year ago
Updated 9 March 2023, over 1 year ago

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

* 🐛 Image styles fails with absolute path in $settings['file_public_path'] Needs work - 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...

📌 Task
Status

Fixed

Version

1.0

Component

Code

Created by

🇦🇹Austria fago Vienna

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

Comments & Activities

  • Issue created by @fago
  • First commit to issue fork.
  • 🇸🇮Slovenia useernamee Ljubljana

    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.

  • Assigned to fago
  • Status changed to Needs review over 1 year ago
  • 🇸🇮Slovenia useernamee Ljubljana
  • @fago opened merge request.
  • Status changed to Needs work over 1 year ago
  • 🇦🇹Austria fago Vienna

    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.

  • 🇦🇹Austria fago Vienna

    > 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';

  • First commit to issue fork.
  • 🇦🇹Austria fago Vienna

    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';

  • 🇷🇸Serbia petar_basic

    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.

  • 🇦🇹Austria fago Vienna

    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.

  • 🇷🇸Serbia petar_basic

    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
      
  • Status changed to Fixed over 1 year ago
  • 🇦🇹Austria fago Vienna

    Perfect, thank you!

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

Production build 0.69.0 2024