Jump to content
MadHatter

Is it possible to get the old value of a field in saveReady?

Recommended Posts

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

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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.

  • Like 1

Share this post


Link to post
Share on other sites

Strange, thought I had the problem some time ago, where it hadn't worked with $page.

Share this post


Link to post
Share on other sites

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?

Share this post


Link to post
Share on other sites

I'd suggest taking a look at VersionControl. The plugin is certainly the goto reference for a "compare field-data" topic.

  • Like 1

Share this post


Link to post
Share on other sites

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() :)

  • Like 3

Share this post


Link to post
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

  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...