title | label | order | desc | keywords |
---|---|---|---|---|
Global Hooks |
Globals |
40 |
Hooks can be added to any Global and allow you to validate data, flatten locales, hide protected fields, remove fields and more. |
hooks, globals, config, configuration, documentation, Content Management System, cms, headless, javascript, node, react, express |
Globals feature the ability to define the following hooks:
All Global Hook properties accept arrays of synchronous or asynchronous functions. Each Hook type receives specific arguments and has the ability to modify specific outputs.
globals/example-hooks.js
import { GlobalConfig } from 'payload/types';
const ExampleHooks: GlobalConfig = {
slug: 'header',
fields: [
{ name: 'title', type: 'text'},
]
hooks: {
beforeValidate: [(args) => {...}],
beforeChange: [(args) => {...}],
beforeRead: [(args) => {...}],
afterChange: [(args) => {...}],
afterRead: [(args) => {...}],
}
}
Runs before the update
operation. This hook allows you to add or format data before the incoming data is validated.
import { GlobalBeforeValidateHook } from 'payload/types'
const beforeValidateHook: GlobalBeforeValidateHook = async ({
data, // incoming data to update or create with
req, // full express request
originalDoc, // original document
}) => {
return data; // Return data to update the document with
}
Immediately following validation, beforeChange
hooks will run within the update
operation. At this stage, you can be confident that the data that will be saved to the document is valid in accordance to your field validations. You can optionally modify the shape of data to be saved.
import { GlobalBeforeChangeHook } from 'payload/types'
const beforeChangeHook: GlobalBeforeChangeHook = async ({
data, // incoming data to update or create with
req, // full express request
originalDoc, // original document
}) => {
return data; // Return data to update the document with
}
After a global is updated, the afterChange
hook runs. Use this hook to purge caches of your applications, sync site data to CRMs, and more.
import { GlobalAfterChangeHook } from 'payload/types'
const afterChangeHook: GlobalAfterChangeHook = async ({
doc, // full document data
previousDoc, // document data before updating the collection
req, // full express request
}) => {
return data;
}
Runs before findOne
global operation is transformed for output by afterRead
. This hook fires before hidden fields are removed and before localized fields are flattened into the requested locale. Using this Hook will provide you with all locales and all hidden fields via the doc
argument.
import { GlobalBeforeReadHook } from 'payload/types'
const beforeReadHook: GlobalBeforeReadHook = async ({
doc, // full document data
req, // full express request
}) => {...}
Runs as the last step before a global is returned. Flattens locales, hides protected fields, and removes fields that users do not have access to.
import { GlobalAfterReadHook } from 'payload/types'
const afterReadHook: GlobalAfterReadHook = async ({
doc, // full document data
req, // full express request
findMany, // boolean to denote if this hook is running against finding one, or finding many (useful in versions)
}) => {...}
Payload exports a type for each Global hook which can be accessed as follows:
import type {
GlobalBeforeValidateHook,
GlobalBeforeChangeHook,
GlobalAfterChangeHook,
GlobalBeforeReadHook,
GlobalAfterReadHook,
} from 'payload/types';