1

I am using services module in Drupal 8 to output node body in JSON format. The inline images are broken in android app which uses json to render node. Is there any code/method or module for D8 which can rewrite relative inline images path to absolute. My drupal version is 8.0.5

  • My drupal version is 8.0.5 you need to update to 8.2.5 – No Sssweat Jan 30 '17 at 9:31
  • These images are inside the body field? added through CKeditor? – No Sssweat Jan 30 '17 at 9:32
  • Yes, added using default ckeditor – Sam Jan 30 '17 at 9:42
  • I tried using Pathologic and rel_to_abs modules, they are able to convert relative url to absolute in browser view but not in serialized JSON output. – Sam Jan 30 '17 at 18:52
  • does core REST API module also not help here? Probably you can write a views serializer plugin to alter JSON output like cgit.drupalcode.org/one_api/tree/src/Plugin/views/style/… – TechNikh Mar 29 at 14:42
2

This is discussed for a long time, but unfortunately there is no solution committed to core until now, see https://www.drupal.org/node/2630076

Basically this tells you, that you need a text filter like this

http://cgit.drupalcode.org/drupal/tree/core/modules/editor/src/Plugin/Filter/EditorFileReference.php

where you change the line

$node->setAttribute('src', file_url_transform_relative(file_create_url($file->getFileUri())));

to

$node->setAttribute('src', file_create_url($file->getFileUri()));

by removing url_transform_relative().

  • How to use this text filter file. My drupal version is 8.0.5 and I and not able to find url_transform_relative() function in EditorFileReference.php file. – Sam Jan 30 '17 at 18:17
  • Don't know if you have the data attributes you need in 8.0.5 - More information how to use and build text filters: lullabot.com/articles/creating-a-custom-filter-in-drupal-8 – 4k4 Jan 30 '17 at 18:50
  • Is there any way to force ckeditor to use absolute url`s for inline images? – Sam Jan 30 '17 at 19:43
  • By default ckeditor adds data-entity-uuid to the link which helps to identify from which file it was generated. So no action is needed. You can check in ckeditor the html source, if your links have the data attribute. Then if you apply the text filter the url will be regenerated on each request. – 4k4 Jan 30 '17 at 19:52
  • Tried using text filters provided by Pathologic and rel_to_abs modules, they were able to regenerate relative url to absolute in browser view but not in serialized JSON output produced by services module. Text filters has no effect on JSON output, the only way is to force ckeditor to save absolute url of inline image while creating a node. – Sam Jan 30 '17 at 20:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.