Tested with blocks and nodes. Images in WYSIWYG body are not replaced with absolute ones.
Problem: In a Views REST Export display with the Fields row plugin (data_field), root-relative URLs in field markup are not rewritten to absolute URLs.
Root cause: DataFieldRow::render() stores $field->advancedRender() output (a MarkupInterface) directly into the row array. The serializer never sees TypedData, so StringDataNormalizer (matching StringInterface) never fires.
Fix: Added ViewsStyleMarkupNormalizer that targets MarkupInterface at priority 10, guarded on isset($context['views_style_plugin']) — the key set by \Drupal\rest\Plugin\views\style\Serializer::render(). It casts to string and runs Html::transformRootRelativeUrlsToAbsolute(). Only Views REST responses are affected; other serializer calls stay untouched.
Issue fork rest_absolute_urls-3054824
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
Comment #2
nicholassI tried for hours to debug and attempt to get this module to work, but I couldn't as well. I used xDebug and stepped through the code but this normalizer never fires for any of my fields on a Rest Export View.
I was able to find a similar module and I attempted to use it to debug why this one was working but I am not very good at this PHP decorator class based stuff. https://github.com/voidagency/vactory_starter_kit/blob/6c31f2555eed2f1ed...
Comment #3
batigolixComment #4
batigolixComment #5
batigolixComment #6
batigolixComment #8
batigolixComment #9
batigolixWith some Claude assistance I created a first draft
Comment #10
batigolixComment #11
batigolixComment #13
arjenk commentedI was puzzled by
According to https://github.com/symfony/serializer/blob/7.4/Normalizer/NormalizerInte... we should return FALSE if "a result cannot be cached when it depends on the context". And guess what: supportsNormalization() uses $context. So changing to FALSE makes sense if this is only for views. This is the small fix for this MR.
However: the purpose of this module is for *all* REST calls, so I went back to TRUE and removed the supportsNormalization() override with the views_style_plugin check. The normalizer now rewrites every MarkupInterface value it receives, the same way StringDataNormalizer already does for string values.
Since i found the class name also a bit confusing ('Style'?) i could now also rename it to MarkupNormalizer.
Verified on Drupal 11.4.5 / PHP 8.4 with a REST export view: the Fields row now returns absolute URLs, Entity row is unchanged. phpcs ok, 25 unit tests pass.
BTW: using getSupportedTypes() means we have to raise the core requirement to "^10.1 || ^11 || ^12". (The rebase gave an conflict on this line.)
Comment #14
batigolix@arjenk I created a 2.1.x branch for D11.3 and higher, so we can drop D10 support on this branch.
I adapted some of your changes in the MR
My junior assistant (want to remain anonymous) also changed the phpunit test : @dataProvider annotations to #[DataProvider] attribute for PHPUnit 12 compatibility
Tested this succesfully on D12
Pipeline is happy
Can you double check all is well? Then we can merge and finish this
Comment #15
arjenk commentedRetested on 2.1.x, Drupal 11.4.7, PHP 8.4.
With a views REST export (fields row), with the MR the href/src's are absolute, all unittests pass, and pipeline is green.
I'm fine with RTBC.
Comment #16
batigolixMany thanks to all the contributors