From 76068637fbd20deb0c85aeca0defe6ede1e002f3 Mon Sep 17 00:00:00 2001 From: Elliot DeNolf Date: Tue, 10 Nov 2020 00:36:48 -0500 Subject: [PATCH] docs(hooks): add examples --- docs/Hooks/examples.mdx | 59 ++++++++++++++++++++++++++++++++++++++++- 1 file changed, 58 insertions(+), 1 deletion(-) diff --git a/docs/Hooks/examples.mdx b/docs/Hooks/examples.mdx index e31b494f04..c7865a78c7 100644 --- a/docs/Hooks/examples.mdx +++ b/docs/Hooks/examples.mdx @@ -4,4 +4,61 @@ label: Examples order: 20 --- -Show common examples of how to write hook functions. +### Collection Hooks + +#### Set createdBy and lastModifiedBy + +```js +beforeChange: [ + ({ req, operation }) => { + if (req.user && req.body) { + if (operation === 'create') { + req.body.createdBy = req.user.id; + } + req.body.lastModifiedBy = req.user.id; + } + } +] +``` +_NOTE: createdBy and lastModifiedBy must be fields on the collection_ + +#### Use Local Payload API + +Queries a separate collection and appends to returned result + +```js +afterRead: [ + async ({ req, doc }) => { + const formattedData = { ...doc }; + const posts = await req.payload.find({ + collection: 'posts', + }); + + formattedData.posts = posts; + + return formattedData; + }, +], +``` + +#### Read Request Header + +```js +beforeRead: [ + ({ req }) => { + if (req.headers.specialheader === 'special') { + // Do something... + } + }, +], +``` + +### Field Hooks + +#### Kebab-case Formatter + +```js +beforeValidate: [ + ({ value }) => val.replace(/ /g, '-').toLowerCase(); +] +```