payloadcms/payload

Changes to hidden fields via hooks are not persisted

Closed this issue · 3 comments

Describe the Bug

See title

Link to the code that reproduces this issue

https://github.com/elliott-w/payload

Reproduction Steps

Add the following field to any collection:

{
  name: 'test',
  type: 'checkbox',
  defaultValue: false,
  hidden: true,
  hooks: {
    beforeChange: [
      () => {
        return true
      },
    ],
    afterChange: [
      ({ value }) => {
        console.log(`------- ${value} -------`)
      },
    ],
  },
},

In the repro, I've added this to the posts collection.

When you create a post you would expect true to be printed in console, but you get false

Change hidden property to false and now you get true printed in console.

Which area(s) are affected? (Select all that apply)

area: core

Environment Info

Binaries:
Node: 20.16.0
npm: 10.8.1
Yarn: N/A
pnpm: 9.12.2
Relevant Packages:
payload: 3.0.0-beta.124
Operating System:
Platform: darwin
Arch: arm64
Version: Darwin Kernel Version 23.6.0: Mon Jul 29 21:13:04 PDT 2024; root:xnu-10063.141.2~1/RELEASE_ARM64_T6020
Available memory (MB): 16384
Available CPU cores: 10

Hey @elliott-w — this is by design, as by default, hidden fields are completely omitted before afterChange hooks are run. They would appear if you had run a local API update or create with showHiddenFields: true, but because the REST API and GraphQL APIs do not have a way to expose hidden fields, they will indeed be hidden in lots of cases.

If you would like to have your hidden field return from APIs and be visible in afterChange hooks, but hide them only in the admin UI, you could use admin.hidden or admin.disabled instead. Either of these options would make them surface in the afterChange hook but stay hidden in the admin UI.

@jmikrut Thanks for the detailed response!

This issue has been automatically locked.
Please open a new issue if this issue persists with any additional detail.