DrQuincy Posted July 14, 2021 Share Posted July 14, 2021 Exactly as the title says: can you know if a page was cloned in a Pages:saveReady hook? I know there is Pages::cloned but can't work out how to do something in cloned that is available in saveReady. I tried useing sessions but it didn't seem to work. So this would be a one-time thing per page. I.e. after the initial save it is no longer regarded as a cloned page. Does that make sense? Is there an easy way to do this? Thanks. Link to comment Share on other sites More sharing options...
bernhard Posted July 15, 2021 Share Posted July 15, 2021 What are you trying to achieve? What is the situation? What is the goal? Link to comment Share on other sites More sharing options...
DrQuincy Posted July 16, 2021 Author Share Posted July 16, 2021 Thanks for your reply. Weird, I replied last night and the reply has gone. Must not have submitted properly. ? I had a hook that was preventing pages that were pointed to via a Page Reference field from being unpublished. It worked fine but when you cloned a page it seems to flag a false positive and was regarded by the system as being linked to from every page. Anyway, I have since found a very simple fix: simply check when the page was created. If less than a few seconds old don't run the hook. This seems to work but I will test more thoroughly. $secondsAgoCreated = time() - $page->created; // Check it's older than 3 seconds; if a clone secondsAgoCreated seems to be 1 if ($secondsAgoCreated > 3) { // Run the hook } Link to comment Share on other sites More sharing options...
dotnetic Posted July 19, 2021 Share Posted July 19, 2021 There is a hook Pages::cloned $this->addHookAfter('Pages::cloned', $this, 'pageCloned') and you can interact with the cloned page: /** * @param HookEvent $event * Method to change the status of a cloned page */ public function pageCloned(HookEvent $event) { // $return = $event->return; $page = $event->arguments(0); $clone = $event->arguments(1); $clone->of(false); $clone->modified = date("Y-m-d H:i:s"); $clone->created_users_id = $this->user->id; $clone->save(array('quiet' => true)); } 1 Link to comment Share on other sites More sharing options...
DrQuincy Posted July 20, 2021 Author Share Posted July 20, 2021 That's very useful, thanks for the code sample. Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now