Jump to content

Can you know if a page was cloned in a Pages:saveReady hook?


DrQuincy
 Share

Recommended Posts

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

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

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));
  }

 

  • Like 1
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...