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

Abierto Apto para principiantes
#279 0 comentarios 0 reacciones 0 asignados Ver en GitHub

Los mantenedores suelen responder en 1 día

Nadie ha tomado este issue todavía.

Evaluación

Dificultad
2/5
Tiempo estimado
1-3 horas
Aptitud para principiantes
82/100
Tipo de issue
Error
Claridad
Bien especificado
Estado de actividad
Activo
Stack tecnológico
typescript
Área
backend

Línea de trabajo

Empieza con packages/payload-plugin-ab/src/utils/injectAdminFields.ts y sigue cómo patchField procesa los campos que se pasan a injectAdminFields. Actualiza el recorrido para los contenedores de diseño indicados en el issue. Luego, lee packages/payload-plugin-ab/tests/utils/injectAdminFields.test.ts y cubre los casos anidados, los grupos con nombre y los casos con campos de admin existentes que se indican. El trabajo está terminado cuando los campos variant se ocultan en los contenedores de diseño compatibles y el caso del grupo con nombre permanece sin cambios.

Escrito por el modelo de indexación a partir del texto del issue.

Descripción

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.

Lenguaje dominante
TypeScript
Estrellas
34
Forks
5
Merge medio
2 d 4 h
PR fusionados (30 d)
41

Preparar el entorno

Este proyecto no incluye contenedor de desarrollo, Dockerfile ni guía de contribución, así que la configuración corre por tu cuenta: empieza por su README y consulta nuestra guía para la primera contribución para los pasos generales.

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Más de focusreactive/payload-plugins

Todos los issues de focusreactive/payload-plugins

Issues similares

Más issues de TypeScript

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.