Files
payload/test/hooks/collections/Data/index.ts
Patrik 458a04b77c feat: expose data argument in afterChange hook for collections and globals (#12756)
### What

This PR updates the `afterChange` hook for collections and globals to
include the `data` argument.

While the `doc` argument provides the saved version of the document,
having access to the original `data` allows for additional context—such
as detecting omitted fields, raw client input, or conditional logic
based on user-supplied data.

### Changes

- Adds the `data` argument to the `afterChange` hook args.
- Applies to both `collection` and `global` hooks.

### Example

```
afterChange: [
  ({ context, data, doc, operation, previousDoc, req }) => {
    if (data?.customFlag) {
       // Perform logic based on raw input
    }
  },
],
```
2025-06-11 06:23:22 -07:00

112 lines
2.7 KiB
TypeScript

import type { CollectionConfig } from 'payload'
export const dataHooksSlug = 'data-hooks'
export const DataHooks: CollectionConfig = {
slug: dataHooksSlug,
access: {
read: () => true,
create: () => true,
delete: () => true,
update: () => true,
},
hooks: {
beforeOperation: [
({ context, collection, args }) => {
context['collection_beforeOperation_collection'] = JSON.stringify(collection)
return args
},
],
beforeChange: [
({ context, data, collection }) => {
context['collection_beforeChange_collection'] = JSON.stringify(collection)
return data
},
],
afterChange: [
({ context, collection }) => {
context['collection_afterChange_collection'] = JSON.stringify(collection)
},
],
beforeRead: [
({ context, collection }) => {
context['collection_beforeRead_collection'] = JSON.stringify(collection)
},
],
afterRead: [
({ context, collection, doc }) => {
context['collection_afterRead_collection'] = JSON.stringify(collection)
return doc
},
],
afterOperation: [
({ args, result, collection }) => {
if (args.req && args.req.context) {
args.req.context['collection_afterOperation_collection'] = JSON.stringify(collection)
}
if (args.req && args.req.context) {
for (const contextKey in args.req.context) {
if (contextKey.startsWith('collection_')) {
result[contextKey] = args.req.context[contextKey]
}
}
}
return result
},
],
},
fields: [
{
name: 'field_collectionAndField',
type: 'text',
hooks: {
beforeChange: [
({ collection, field, context, value }) => {
context['field_beforeChange_CollectionAndField'] =
JSON.stringify(collection) + JSON.stringify(field)
return value
},
],
afterRead: [
({ collection, field, context }) => {
return (
(context['field_beforeChange_CollectionAndField'] as string) +
JSON.stringify(collection) +
JSON.stringify(field)
)
},
],
},
},
{
name: 'collection_beforeOperation_collection',
type: 'text',
},
{
name: 'collection_beforeChange_collection',
type: 'text',
},
{
name: 'collection_afterChange_collection',
type: 'text',
},
{
name: 'collection_beforeRead_collection',
type: 'text',
},
{
name: 'collection_afterRead_collection',
type: 'text',
},
{
name: 'collection_afterOperation_collection',
type: 'text',
},
],
}