Jump to content

Check if a page is new within the save hook


ffub
 Share

Recommended Posts

It's pretty easy if you are doing a before hook because the page's 'id' will be 0.

But I'm guessing you need an after hook. ProcessWire doesn't populate the page's 'created' date until after it's been loaded from the database the first time. As a result, your after save hook can check the $page->created property. If it's empty, then the page is new.

  • Like 1
Link to comment
Share on other sites

  • 1 year later...

Old thread I know. But this is not working with me because it when $page created doesn't exist yet, the fields are also not populated. I need to do something the first time a page is saved after the editor had the chance to fill in the fields. Isn't there a "publish" hook? I can't find it...

Link to comment
Share on other sites

Thanks Ryan, but no. This only triggers the action when the page is created, before the editor had any chance to fill the info I need. For, now what I did was to add a hidden check box that the module changes to 1 right after the action was performed, this deactivates future actions on this page. The module also checks if the page is published and if the needed field is not empty.

if($page->get($this->check) !== 1 && $page->status === 1 != "" && $page->get($this->message) != "")

Unrelated: I just noticed a small incoherence with the page saving interface. If a page is unpublished, and you go to "settings" and uncheck the "Unpublished: Not visible on site" checkbox and then press on the button "Save + Keep Unpublished", the page is published. Of course we can assume that someone that unchecks that box wants the page to be published, but but it's still incoherent. Maybe it would be enough if the button would change to "publish" when that checkbox is unchecked, that's easily achieved with an event handler in javascript.

edit: the button on the bottom should also be removed.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...