Pass draft to collection beforeChange and beforeValidate hooks #18249
fabioquarantini
started this conversation in
Feature Requests & Ideas
Replies: 0 comments
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.
Problem
A collection
beforeChangehook cannot tell a draft save from an unpublish.Both reach the hook with
data._status === 'draft': on a draft saveupdateDocumentrewritesdata._statusto'draft'before the hooks run, andoriginalDocis the latest version rather than the published document. From inside the hook the two calls look identical. The only hook that knows whether the main document is about to be written isbeforeOperation, which receivesargs.draft.Why it matters
A hook that protects what the site serves has to let draft saves through and block unpublishes. Our case: pages have nested URLs (
/parent/child), so a parent must not be unpublished while it still has published children. Saving a draft of the parent is fine, because the main document is untouched; unpublishing it is not.Today the only way is a
beforeOperationhook that copiesargs.draftintoreq.context, mirroring Payload's own condition (draft && hasDraftsEnabled && data._status !== 'published'), plus abeforeChangehook that reads it back. That is two hooks that only work when mounted together, and a copy of an internal rule that can drift on upgrade.Proposal
Pass the value Payload already computes to the collection
beforeValidateandbeforeChangehooks:draftwould be whatupdateDocumentcallsisSavingDraft.createandrestoreVersionreach the same hooks and know the answer too.Notes
mainis unchanged.All reactions