payloadcms / payloadcms/payload

Relationship field option groups duplicate when collection labels are localized objects

Open Beginner friendly
#17,758 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

area: ui
Dominant language
TypeScript
Stars
44.8k
Forks
4.2k
Avg merge
2d 21h
Merged PRs (30d)
53

Description

Describe the Bug

In packages/ui/src/fields/Relationship/optionsReducer.ts, option groups are created with the translated label but looked up by the untranslated one:

// creating (line 87) — translated
newOptions.push({
  label: getTranslation(collection.labels.plural, i18n),
  options: ...,
})

// looking up (lines 37, 127, 156) — NOT translated
newOptions.find((optionGroup) => optionGroup.label === collection.labels.plural)

When labels.plural is a localization object such as { en: 'Case Studies', zh: '客户案例' }, the lookup compares an object against a string, never matches, and every batch of loaded documents appends yet another group carrying the same title.

The near-identical reducer at packages/ui/src/elements/WhereBuilder/Condition/Relationship/optionsReducer.ts (lines 45, 69) wraps both sites in getTranslation, which suggests this is an oversight rather than intentional.

Three call sites are affected:

Case Line Variable
ADD 37 optionsToAddTo
REMOVE 127 indexOfGroup
UPDATE 156 foundOptionGroup

Separately, the REMOVE case does not destructure i18n from the action even though the REMOVE type in types.ts:57-63 declares it and Input.tsx dispatches it.

Consequences beyond the duplicated headings: because REMOVE and UPDATE never find their group, deleting a document leaves its stale option in the dropdown, and renaming one does not refresh the displayed title.

Reproduction Steps
  1. Give a collection a localized plural label:
export const CaseStudies: CollectionConfig = {
  slug: 'case-studies',
  labels: {
    singular: { en: 'Case Study', zh: '客户案例' },
    plural: { en: 'Case Studies', zh: '客户案例' },
  },
  // ...
}
  1. Add a relationship field pointing at it — a polymorphic one makes it most obvious:
{
  name: 'ref',
  type: 'relationship',
  relationTo: ['case-studies', 'products', 'posts'],
}
  1. Open the field's dropdown in the admin panel and scroll so more than one batch loads.

Expected: one group heading per collection.

Actual: the same headings repeat — Case Studies / Products / Case Studies / Products / … — with the documents scattered across the duplicates.

Collections whose labels are plain strings are unaffected, since getTranslation passes strings through unchanged and the comparison happens to succeed.

Which area(s) are affected?

area: ui

Environment Info

Reproduced on 3.86.0. The code is byte-identical on main and in 3.88.0, so the issue is current.

Suggested fix
-      const optionsToAddTo = newOptions.find(
-        (optionGroup) => optionGroup.label === collection.labels.plural,
-      )
+      const optionsToAddTo = newOptions.find(
+        (optionGroup) => optionGroup.label === getTranslation(collection.labels.plural, i18n),
+      )

applied at all three lookup sites, plus destructuring i18n in the REMOVE case. Happy to open a PR.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start in packages/ui/src/fields/Relationship/optionsReducer.ts and compare the three group lookups at lines 37, 127, and 156 with the translated label creation at line 87. Check the analogous reducer at packages/ui/src/elements/WhereBuilder/Condition/Relationship/optionsReducer.ts, including its handling of i18n in REMOVE. Done means localized collection labels keep one option group while ADD, REMOVE, and UPDATE continue to work.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
frontend, internationalization
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Quiet
Clarity
Clearly specified
Newbie friendliness
78/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.