payload icon indicating copy to clipboard operation
payload copied to clipboard

Live preview does not correctly find relations when using blocks inside the lexical rte.

Open stefvw93 opened this issue 11 months ago • 3 comments

Link to reproduction

No response

Describe the Bug

The following line in the rich text traverser detects relations based on the relationTo and value fields inside any given iterated object in a rich text tree: https://github.com/payloadcms/payload/blob/c038acb3a4da6cda85d034aeb8b042d172ee868f/packages/live-preview/src/traverseRichText.ts#L59

However, when using the blocks feature in the lexical rte, relationships are not defined by those fields, and instead simply hold an id string reference field.

Because of this, the rich text traverser is never able to resolve relation fields inside blocks.

To Reproduce

  1. Enable lexical editor field on some collection.
  2. Use the Blocks feature
  3. Create a block with a type: 'relation' field, with a relationTo some other collection.
  4. Populate the field in the admin UI
  5. Implement the live preview feature, set depth > 0
  6. Check the data returned from the callback function
  7. The relation field in the respective block will always just havr a <fieldname>: <id> entry, regardless of the depth parameter.

Payload Version

2.11.2

Adapters and Plugins

@payloadcms/[email protected]

stefvw93 avatar Mar 10 '24 17:03 stefvw93

Hey

Any news on the solution for this problem ?

100tomtomas100 avatar Apr 23 '24 17:04 100tomtomas100

I've found a workaround for this. Just set relationTo to an array, even if you only need to reference a single collection, like relationTo: ['images'] instead of relationTo: 'images'.

In this way the resulting field values will have the form of { relationTo: <collection-slug>, value: <resolved-document> }, which is resolved correctly by traverseRichText.

abaco avatar Aug 13 '24 12:08 abaco