Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
When fetching a rest resource and getting the body field images come with a relative path. specifically "inline images". Something like
"installation_path/sites/default/files/inline-images".
This makes it a bit difficult to actually display the images if we are for example doing a decoupled site and need to render the body. Then we would some heavy engineering to actually display the absolute URL to the image so that it is actually displayed.
We can be fetching an image from a different domain and so absolute paths wont work at all..Is there any option to deal with this?
Best regards
Joao Garin
Comments
Comment #2
swentel CreditAttribution: swentel commentedhttps://www.drupal.org/project/pathologic might fix this, although I'm not sure 100% sure.
Wondering if this belongs for editor/ckeditor component ..
Comment #3
Wim LeersNot heavy engineering, simple string replacement.
Look at
Drupal.url()
indrupal.js
.Comment #4
Wim LeersOh, this is not talking about image fields, but with
inline images in formatted text fields
. That's using relative URLs very much intentionally.This is then most definitely not a bug, but a support request.
There are three clear options (and probably more):
Drupal.url()
JavaScript function.\Drupal\editor\Form\EditorImageDialog::submitForm()
does.data-entity-type
anddata-entity-uuid
attributes on the<img>
tag and use those to load theFile
entity and build an absolute URL.Comment #5
TwoDSeems like I should have taken more part in D8 development...
"Hardcoding" any URL at all in WYSIWYG editors feels like pure madness to me. The main reason I use Media module is that it references file ids and not paths as the domain used to access the site is irrelevant. Everything "just works" when URLs are inserted during rendering as there are never any hardcoded paths polluting the database.
Hardcoded paths now break every time when moving between dev/staging/live sites because they use different sites/[sitename] paths...
Having media (only images really) managed by Core is great, but this alone makes it pretty much useless unless my site is extremely simple and never ever moves to a new URL.
Comment #6
Wim LeersI think #5 is a gross exaggeration.
multi-site aliases
Most sites have different domains (hosts) when they change from dev -> staging -> prod. Only some of them being in a subdir is rare, and not wise, because it means you're probably missing things in QA.
Moving from
dev.example.com
toexample.com
is of course totally valid, but then it's up to you to set up yoursites.php
file correctly:dev.example.com
andstaging.example.com
should point toexample.com
. That means files will be uploaded to/sites/example.com/files
always, regardless of dev/staging/prod. That's even exactly what/sites/example.sites.php
documents!Storing vs. generating URLs
We went with a practical compromise in core: use relative file URLs, because that works fine 99% of the time (see above explanation).
But, we also thought about the other 1%. Whenever you insert an image, there are
data-entity-type
anddata-entity-uuid
as well. So you can do reliable transformations. You can create a filter that looks for those attributes, loads the file entity, generates a URL for the current site. It's trivial to do so.In fact, that's where Media module got it from! This metadata (data- attributes) is also how images uploaded via the text editor show up in the file reference/usage tracking (see
\editor_entity_insert()
& friends). And how we're able to ensure that changes to those files are reflected immediately (see\Drupal\editor\Plugin\Filter\EditorFileReference
).Going forward
I think it would be okay to update
\Drupal\editor\Plugin\Filter\EditorFileReference()
to automatically update thosesrc
attributes on images. Then even your 1% use case is taken care of. If you want to do that, please create a new issue referencing this issue, please don't repurpose this one.