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

オープン 初心者向け
#279 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る

メンテナーはふだん 1 日以内に返信

まだ誰も着手していません。

評価

難易度
2/5
見積もり時間
1〜3時間
初心者へのやさしさ
82/100
issue の種類
バグ
明瞭さ
明確に書かれている
活発さ
活発
技術スタック
typescript
領域
backend

調査の方向性

packages/payload-plugin-ab/src/utils/injectAdminFields.tsから始め、patchFieldがinjectAdminFieldsに渡されたフィールドをどのように処理するかを追ってください。issueに記載されたレイアウトコンテナに対応するよう、走査処理を更新してください。次に、packages/payload-plugin-ab/tests/utils/injectAdminFields.test.tsを読み、記載されたネストされたケース、名前付きグループのケース、既存のadminフィールドがあるケースを網羅してください。対応するレイアウトコンテナ内でvariantフィールドが非表示になり、名前付きグループのケースが変更されない状態になれば完了です。

索引モデルが issue の本文から書いたものです。

説明

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.

主要言語
TypeScript
スター
34
フォーク
5
平均マージ
2日 4時間
マージ済み PR(30日)
41

環境構築

このプロジェクトには開発コンテナ、Dockerfile、コントリビューションガイドがありません。まず README を読み、一般的な手順ははじめてのコントリビューションガイドを参照してください。

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

focusreactive/payload-plugins のほかの issue

focusreactive/payload-plugins の issue をすべて見る

似ている issue

TypeScript の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。