[payload-plugin-ab]: the slug and tenant fields stay editable on variants when they sit inside a layout field (row, collapsible, unnamed group or tab)

Open Beginner friendly
#279 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
82/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
typescript
Domain
backend

Research direction

Start with packages/payload-plugin-ab/src/utils/injectAdminFields.ts and trace how patchField handles fields passed to injectAdminFields. Update the traversal for the layout containers listed in the issue, then read packages/payload-plugin-ab/tests/utils/injectAdminFields.test.ts and cover the listed nested, named-group, and existing-admin cases. Done when variant fields in supported layout containers are hidden and the named-group case remains unchanged.

Written by the indexing model from the issue text.

Description

TL;DR

The plugin hides the bucket field (slugField) and the tenant field on variant documents, but it looks for them only among a collection's top-level fields. When a collection puts the field inside a layout-only container (a row, a collapsible, an unnamed group or an unnamed tab), nothing is hidden and no warning is logged. Editors then see the variant's <slug>--<id> value and can change it, which breaks the variant's bucket. The data path is unchanged, because those containers add no data level, so the plugin's reads and writes still work. Only the admin condition is lost. The fix is a recursive patchField that walks layout containers, plus a unit test.

For the implementing agent

Scope
  • Package: packages/payload-plugin-ab (@focus-reactive/payload-plugin-ab), seen in 2.10.1, still present in 2.11.1.
  • File: packages/payload-plugin-ab/src/utils/injectAdminFields.ts. New test: packages/payload-plugin-ab/tests/utils/injectAdminFields.test.ts.
  • Line numbers refer to commit 2065eea8.
Root cause

injectAdminFields.ts:29-34:

/** Find a top-level field by name and patch it. Returns the new fields array. */
function patchField(fields: Field[], name: string, patcher: (f: Field) => Field): Field[] {
  return fields.map((f) => {
    if ("name" in f && f.name === name) return patcher(f);
    return f;
  });
}
  • It is called for the bucket field at line 94 and for the tenant field at line 99.
  • A field nested in { type: "group", label: "URL", admin: { position: "sidebar" }, fields: [slug, ...] } is never matched, so hideOnVariant (lines 17-26) is never applied.
  • The plugin reads and writes the value at the document's top level (variantDoc[slugField] in generateVariantData, parentDoc[slugField] and [slugField]: `${originalSlug}--${nanoid()}` in the duplicate-variant endpoint). Layout containers keep that path, so only the hiding fails.
Change

Make patchField recurse into containers that do not add a data level, and stop at the first match:

  • row, collapsible: recurse into fields
  • group without name: recurse into fields
  • tabs: recurse into each tab without name

Do not recurse into named groups, named tabs, arrays or blocks. A field there has a different data path, and hiding it would hide an unrelated field with the same name.

Optional: log a warning (once per collection) when slugField matches no field. Today a typo or a moved field fails silently.

Test

tests/utils/injectAdminFields.test.ts. For each case, call injectAdminFields and assert that the matched field's admin.condition returns false for { _abVariantOf: 1 } and true for {}:

  1. slug at the top level (current behaviour, must keep passing).
  2. slug inside an unnamed sidebar group.
  3. slug inside a collapsible inside an unnamed tab.
  4. slug inside a named group meta: no condition is added, and the top-level fields are unchanged.
  5. An existing admin on the field (e.g. description, position) is kept.
Reproduction
  1. Add a collection to the plugin's collections with the slug field in a sidebar group:

    fields: [
      {
        type: "group",
        label: "URL",
        admin: { position: "sidebar" },
        fields: [{ name: "slug", type: "text", required: true }],
      },
      { name: "title", type: "text" },
    ]
    
  2. Create a document, then create an A/B variant from the admin.

  3. Open the variant: the URL group shows slug with <slug>--<id>, editable. Expected: the field is hidden, as it is when slug is a top-level field.

Workaround

Keep slugField (and the tenant field) as a top-level field with admin.position: "sidebar", outside any group.

Dominant language
TypeScript
Stars
34
Forks
5
Avg merge
2d 4h
Merged PRs (30d)
41

Getting set up

This project ships no dev container, Dockerfile or contributing guide, so setting up is up to you: start from its README, and see our first-contribution guide for the general steps.

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.

More from focusreactive/payload-plugins

All issues in focusreactive/payload-plugins

Similar issues

More TypeScript issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.