Jump to content

How to get a list of all changes on page save?


bernhard
 Share

Recommended Posts

This is what I have so far:

$wire->addHookAfter('Page(template=contact|sample)::changed', function(HookEvent $event) {
  $page = $event->object;
  $title = $page->title;
  $change = $event->arguments(0);
  $old = $event->arguments(1); // old value
  $new = $event->arguments(2); // new value

  $text = '...';

  $mail = new WireMail();
  $mail->to(['...']);
  $mail->from('...');
  $mail->subject("Changed page: $title");
  $mail->bodyHTML($text);
  $mail->send();
});

The problem is that this will send me an e-mail for every changed field, eg "Changed page X, field A" and "Changed page X, field B". But I want one single E-Mail that says "Changed page X, field A and B".

getChanges() on the other hand (https://processwire.com/api/ref/page/get-changes/) does get me an array of changes, but without their old and new values! It should be possible using https://processwire.com/api/ref/wire/set-track-changes/ but where/how would I set this?

Thx

Link to comment
Share on other sites

Unfortunately not... I'm changing fields tmp1+2 from X to XX and Y to YY:

This is what I tried in site/ready.php

$wire->addHookBefore('Pages::saveReady', function($event) {
  $page = $event->arguments(0);
  $page->setTrackChanges(Wire::trackChangesValues);
});

$wire->addHookAfter('Pages::saveReady', function($event) {
  $page = $event->arguments(0);
  bd($page->getChanges(true), 'getChanges');
});

$wire->addHookAfter('Pages::saved', function($event) {
  bd($event->arguments(1), 'changes');
  bd($event->arguments(2), 'values');
});

$wire->addHookAfter('Page::changed', function(HookEvent $event) {
  bd($event->arguments(0), 'changed(0)');
  bd($event->arguments(1), 'changed(1)');
});

OCURkkA.png

6MCotvH.png

5DIMtKZ.png

Link to comment
Share on other sites

Have you tried

wire()->addHookBefore('Pages::save', function ($event) {
    $p = $event->arguments(0);
    // Get the previous version of the page
    $oldPage = wire('pages')->getById($p->id, array(
    	'cache' => false, 			// don't let it write to cache
    	'getFromCache' => false, 	// don't let it read from cache
    	'getOne' => true, 			// return a Page instead of a PageArray
    ));

Then you can compare the old and new.

Link to comment
Share on other sites

Another approach to try:

// Store page changes in custom property on $wire
$wire->addHookAfter('Page(template=contact|sample)::changed', function(HookEvent $event) {
	$page = $event->object;
	$what = $event->arguments(0);
	$old = $event->arguments(1);
	$new = $event->arguments(2);

	$page_changes = $event->wire('page_changes') ?: [];
	$page_changes[$page->id][$what] = [
		'old' => $old,
		'new' => $new,
	];
	$event->wire('page_changes', $page_changes);
});

// Send email if there are page changes after the request is finished
$wire->addHookAfter('ProcessWire::finished', function(HookEvent $event) {
	$page_changes = $event->wire('page_changes');
	if($page_changes) {
		// Send the email using the values in $page_changes ...
	}
});

 

  • Like 3
  • Thanks 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...