Skip to content
Discussion options

You must be logged in to vote

@cerize Hi! You can use the beforeChange hook to check the document's _status. If it went from published to draft then you can throw an API error. Something like this (untested):

import APIError from "payload/dist/errors/APIError";

const beforeChangeHook: GlobalBeforeChangeHook = async ({
  data, // incoming data to update or create with
  req, // full express request
  originalDoc, // original document
}) => {
  if (originalDoc._status === 'published' && data._status !== 'published') throw new APIError('You cannot do this!');
  return data; // Return data to update the document with
}

Here are the docs for this hook: https://payloadcms.com/docs/hooks/globals#beforechange

Replies: 1 comment 7 replies

Comment options

You must be logged in to vote
7 replies
@jacobsfletch
Comment options

@cerize
Comment options

@DanRibbens
Comment options

@jacobsfletch
Comment options

@cerize
Comment options

Answer selected by zubricks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants