Jump to content


Photo

Accessing previous version of page before/after page save


  • Please log in to reply
3 replies to this topic

#1 Pete

Pete

    Administrator

  • Administrators
  • 1,756 posts
  • 657

  • LocationChester, England

Posted 16 March 2012 - 06:12 AM

Is it possible to do this inside a module?

I tried before page save to get the page using something like $previousVersion = wire('pages')->get($page->id); but that didn't work (and certainly wouldn't work after page save ;)).

It's probably something simple like $_POST vars I bet, but just wanted to check.

#2 ryan

ryan

    Hero Member

  • Administrators
  • 5,771 posts
  • 3112

  • LocationAtlanta, GA

Posted 16 March 2012 - 09:41 AM

ProcessWire keeps a memory cache of pages loaded, so when you retrieve a page, its going to be the same instance of that page when you retrieve it elsewhere. But once you save a page (in the API) that memory cache gets cleared. If you wanted to keep an in-memory copy of it before it was saved, you'd want to clone it sometime before the page is saved:

$pageCopy = clone $page;

From that point forward, any changes to $page won't be seen in $pageCopy. This clone will cease to exist once it is out of memory scope.

For obvious reasons, file-based assets aren't cloned here. So if your clone depends on file/image assets, then you have to create a new page not just in memory, but on database/disk too. Here's how you do that:

$pageCopy = $pages->clone($page);

That does a recursive clone, cloning not just that page, but any children too. See the parameters to the $pages->clone() function for additional options to control this. You can also specify that you want it to clone to a different parent. Be careful with the cloning as it's something you probably don't want to do on every page save. It can be an expensive operation if there are lots of files or subpages associated with the cloned page.

#3 Pete

Pete

    Administrator

  • Administrators
  • 1,756 posts
  • 657

  • LocationChester, England

Posted 16 March 2012 - 10:02 AM

So is there no way when you click Save on a page to have a module access the page's field contents as they were before you hit Save or are you saying that even before save the copy of that page in the memory would be the new one even if it's not saved yet? Surely the old copy in the database should be accessible somehow since we're hookig before page save without the need to clone it?

Literally all I want to do is compare the value of a field before the person hit Save and what the value is after they click Save by using a beforePageSave hook if that makes sense.

#4 ryan

ryan

    Hero Member

  • Administrators
  • 5,771 posts
  • 3112

  • LocationAtlanta, GA

Posted 16 March 2012 - 10:06 AM

Okay I think I understand what you are saying. So you want to just load a fresh copy from the database, not keep an existing copy at some state in memory. Do this to load a fresh copy from the database:

wire('pages')->uncacheAll();
$oldPage = wire('pages')->get($page->id); 





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users