BUG: Collection hooks can't determine when document gets unpublished #2627
johannesschaffer
started this conversation in
Feature Requests & Ideas
Replies: 1 comment 2 replies
-
You could check whether this doc has a published version by doing something like this in your hook: const afterChange: CollectionAfterChangeHook = async ({ doc, req: { payload }, previousDoc }) => {
if (doc._status === 'draft' && previousDoc._status === 'published') {
const publishedDoc = await payload.findByID({
collection: collectionConfig.slug,
id: doc.id,
draft: false,
});
if (!publishedDoc || publishedDoc._status === 'draft') {
console.log('Document was unpublished');
} else {
console.log('Just the first draft after publishing')
}
}
return doc;
} |
Beta Was this translation helpful? Give feedback.
2 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
In earlier versions of payload, the _status was either: "published", "draft", "unpublished". At some point the "unpublished" status got removed. I can't check for unpublished by
doc._status == 'draft' && prevDoc._status == 'published'
, since this would also triggger for a normal draft save, when not unpublishing it.Beta Was this translation helpful? Give feedback.
All reactions