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

joaogarin created an issue. See original summary.

swentel’s picture

https://www.drupal.org/project/pathologic might fix this, although I'm not sure 100% sure.
Wondering if this belongs for editor/ckeditor component ..

Wim Leers’s picture

Priority: Normal » Minor

Then we would some heavy engineering to actually display the absolute URL to the image so that it is actually displayed.

Not heavy engineering, simple string replacement.

Look at Drupal.url() in drupal.js.

Wim Leers’s picture

Title: Inline images absolute url » Inline images: how to calculate the absolute file URL
Category: Bug report » Support request
Priority: Minor » Normal
Status: Active » Fixed
Related issues: +#2099205: When uploading and inserting an image trough the WYSIWYG plugin a relative path should be used for the image source (src)

Oh, 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):

  1. String manipulation — look at the Drupal.url() JavaScript function.
  2. Override what \Drupal\editor\Form\EditorImageDialog::submitForm() does.
  3. Parse the data-entity-type and data-entity-uuid attributes on the <img> tag and use those to load the File entity and build an absolute URL.
TwoD’s picture

Status: Fixed » Active

Seems 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.

Wim Leers’s picture

Status: Active » Fixed

I 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 to example.com is of course totally valid, but then it's up to you to set up your sites.php file correctly: dev.example.com and staging.example.com should point to example.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 and data-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 those src 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.

Status: Fixed » Closed (fixed)

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