MadHatter Posted January 27, 2015 Share Posted January 27, 2015 Hi, I'm trying to create a changelog module but I can't figure out if it's possible to access the field values before they are saved. I assume saveReady is run just before the page is saved, so I should be able to get the page with it's old values, but so far I haven't had any success. Thanks, MadHatter Link to comment Share on other sites More sharing options...
LostKobrakai Posted January 27, 2015 Share Posted January 27, 2015 Take a look at this, it's related to what you're trying to build. https://processwire.com/talk/topic/4263-effects-of-uncaching-a-page/ Link to comment Share on other sites More sharing options...
adrian Posted January 27, 2015 Share Posted January 27, 2015 Not sure if it suits your needs or not, but: http://modules.processwire.com/modules/process-changelog/ Also, check out isChanged(): http://cheatsheet.processwire.com/?filter=ischanged Link to comment Share on other sites More sharing options...
MadHatter Posted January 27, 2015 Author Share Posted January 27, 2015 Hi guys, I've looked at both of thos posts but I still could not seem to get the original data out. Here are my functions so far: public function init() { $this->log = new FileLog(wire('config')->paths->logs,'changelog'); $this->addHookAfter('Pages::saveReady', $this, 'addToChangelog'); } protected function addToChangelog(HookEvent $event){ $page = $event->arguments[0]; $oldPage = wire('pages')->get($page->id); $oldPage->uncache(); foreach($page->fields as $field){ $value = $page->get($field); $this->log->save($oldPage->get($field).'=>'.$page->get($field)); } } When I try saving a page with new data I just get the entered values on either side of the log line. Link to comment Share on other sites More sharing options...
teppo Posted January 27, 2015 Share Posted January 27, 2015 You're right in that saveReady happens just before Page is saved, but the values of this Page object have already changed. Database contains old values, Page object contains new values, so if you ask the Page for any of it's fields, it'll return the values as they are now (already changed). This is one of the reasons why my Version Control module, for an example, actually stores current values instead of old values. On the next change+save it stores next set of values, and so on. For my use case this didn't make much of a difference, and it was (at the moment) only simple way to do it. That's probably something you could do here too; otherwise you'll have to hook at some point where the values are sure to be unchanged, store them, and later compare them with changed values at saveReady (or find a way to create a copy of current page and return it to database defaults while leaving the ready-to-be-saved Page object intact, though if your uncache trick above isn't doing this, I'm not entirely sure how to do it). Another thing you could check out would be the (relatively new, 2.4+) ___changed() method. This commit contains all the changes related to this feature. 1 Link to comment Share on other sites More sharing options...
LostKobrakai Posted January 27, 2015 Share Posted January 27, 2015 One issue in your code is that uncache is a method of $pages not $page. Link to comment Share on other sites More sharing options...
teppo Posted January 27, 2015 Share Posted January 27, 2015 One issue in your code is that uncache is a method of $pages not $page. Not exactly: $pages->uncache($page) calls $page->uncache(). Both have it, scope is slightly different Link to comment Share on other sites More sharing options...
LostKobrakai Posted January 27, 2015 Share Posted January 27, 2015 Strange, thought I had the problem some time ago, where it hadn't worked with $page. Link to comment Share on other sites More sharing options...
Ivan Gretsky Posted September 1, 2015 Share Posted September 1, 2015 That's probably something you could do here too; otherwise you'll have to hook at some point where the values are sure to be unchanged, store them, and later compare them with changed values at saveReady (or find a way to create a copy of current page and return it to database defaults while leaving the ready-to-be-saved Page object intact, though if your uncache trick above isn't doing this, I'm not entirely sure how to do it). Could you please suggest a hook to store the value of a field to later compare it with the one got from the $page suitable both for frontend and admin? Link to comment Share on other sites More sharing options...
LostKobrakai Posted September 1, 2015 Share Posted September 1, 2015 I'd suggest taking a look at VersionControl. The plugin is certainly the goto reference for a "compare field-data" topic. 1 Link to comment Share on other sites More sharing options...
renobird Posted September 1, 2015 Share Posted September 1, 2015 This may help — either directly, or pick it apart to do what you need. http://modules.processwire.com/modules/markup-activity-log/ 1 Link to comment Share on other sites More sharing options...
Pete Posted November 5, 2015 Share Posted November 5, 2015 I've come back to this topic myself about 3 years after having a similar need in a different thread. This code, from Tom's activity log module and altered slightly, gets both an old and new version of the page when hooking before saveReady: $clone = clone($page); $this->pages->uncache($clone); $old = $this->pages->get($clone->id); // Now just echo both the fields you are interested in: echo $old->field_of_interest . " becomes " . $page->field_of_interest; exit; On save, that should give you access to the old and new versions of the page where you want to compare just one or a few fields and you know which ones you're interested in already. Seems to simple now when you see the use of clone() 4 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