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.

Command icon 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

simoneb created an issue. See original summary.

nicholass’s picture

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

batigolix’s picture

Issue tags: +finalist-sprint
batigolix’s picture

Issue tags: -finalist-sprint
batigolix’s picture

Issue tags: +finalist-sprint
batigolix’s picture

Version: 8.x-1.0-beta1 » 2.0.0

batigolix’s picture

Status: Active » Needs review
batigolix’s picture

With some Claude assistance I created a first draft

batigolix’s picture

Issue summary: View changes
batigolix’s picture

Issue summary: View changes

arjenk made their first commit to this issue’s fork.

arjenk’s picture

I was puzzled by

+  public function getSupportedTypes(?string $format): array {
+    return [MarkupInterface::class => TRUE];
+  }

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

batigolix’s picture

@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

arjenk’s picture

Status: Needs review » Reviewed & tested by the community

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

batigolix’s picture

Status: Reviewed & tested by the community » Fixed

Many thanks to all the contributors

Now that this issue is closed, review the contribution record.

As a contributor, attribute any organization that helped you, or if you volunteered your own time.

Maintainers, credit people who helped resolve this issue.

  • batigolix committed ba337a93 on 2.1.x
    Issue #3054824: Rewrite URLs for Views REST Export MarkupInterface...

Status: Fixed » Closed (fixed)

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