Simplify your stack and build anything. Or everything.
Build tomorrow’s web with a modern solution you truly own.
Code-based nature means you can build on top of it to power anything.
It’s time to take back your content infrastructure.

Hook Only on First Publish: Tips Needed

default discord avatar
zed0547last year
12

Hey

@355314072910036993

,



Let's walk through this together here



You want a hook to fire on the first publish of a doc. When you say first publish, do you mean when this doc changes status from a draft to publish, or when this doc gets first created?



Or some mix of both?

  • default discord avatar
    hunaltair69last year

    when it gets first publish



    basically a user creates a building trough a form in the application



    the admins checks it in payload, maybe fixes, adds some details, and makes the first publish



    then the user who created it should get an email that his building was published and it's live

  • default discord avatar
    zed0547last year

    So on that collection, you'd likely want to create a

    beforeChange

    hook.

    https://payloadcms.com/docs/hooks/collections#beforechange

    In that hook, you get the doc before its saved, and the originalDoc - this is how you can compare the

    _status

    of the docs. If the doc is changing from a draft to published, then the originalDoc will have status of draft, and the doc will have a status of published



    Does that make sense?

  • default discord avatar
    hunaltair69last year

    hmmm, it does, but I will have to test it out, thanks for the tip



    I looked into it and my issue is that if the document gets published but after it they make any change, save it as draft and publish it again



    that is the same thing.

  • default discord avatar
    zed0547last year

    You can also have a check for if the doc was previously published



    The website template does this

  • default discord avatar
    hunaltair69last year

    you mean this?

  • default discord avatar
    zed0547last year

    Yes exactly

  • default discord avatar
    hunaltair69last year

    yes but this only checks if exactly the previous was published and now it's unpublished. no?


    like i want to send an email if this is the ever ever first time publishing, and I guess no clear solution on that

  • default discord avatar
    zed0547last year

    I imagine you could make a request via rest or local api against the versions collection to check if any previous version had a status of published

  • default discord avatar
    hunaltair69last year

    yes this was an amazing idea. if anyone else needs this, in the beforeChange hook

  • default discord avatar
    zed0547last year

    Woohoo, very nice! I guess we can mark this as solved then?

  • default discord avatar
    hunaltair69last year

    Yes sir, thank you for the help 😄

  • default discord avatar
    zed0547last year

    My pleasure! Glad we figured it out here

Star on GitHub

Star

Chat on Discord

Discord

online

Can't find what you're looking for?

Get dedicated engineering support directly from the Payload team.