Juergen

Tip how to track changes in field values (before and after)

Recommended Posts

Sometimes you need to track changes in certain values of a page to take an action afterwards.

In this example I will show you how to show a simple message after changes to inform the user that field values have been changed.

Put this piece of code inside your ready.php.

//Track changes
$pages->addHookAfter('saveReady', function($event)
{
  $page = $event->arguments[0];  
  if(!in_array($page->template->name, array('template1','template2'))) return; 
  $fields = array('field1','field2','field3','field4'); 
  $changedfieldsarray = array();
  foreach($fields as $field) {
   if ($page->isChanged($field)) {
     $changedfieldsarray[] = $page->fields->get($field, true)->label;
   } 
  }
  $fieldnames = implode(", ", $changedfieldsarray);
  $this->warning(sprintf(__("Following fields have been changed: %s"), $fieldnames ));
});

The first restriction in this example is to run this hook "aftersaveReady" and only on certain templates: in this case template1 and template 2. You need to rename this to your template names. Remove the following line of code if you want it to run on all pages (no restrictions):

if(!in_array($page->template->name, array('template1','template2'))) return;

Now you have to define the fields where you want to track changes (take a look at this line):

$fields = array('field1','field2','field3','field4');

In this case the fields are called "field1 to field4". Rename it to your field names.

The API call to track changes is "$page->isChanged('fieldname')". You will find more information at https://processwire.com/api/ref/page/is-changed/. Use a foreach to loop through the fields array and store all changed fields inside an array.

In the last step all the changed fields were outputted inside a message to inform the user. In this case I want to show the labels instead of the field names.

Only to mention: The changes will keep alive, so you have to delete it manually (http://cheatsheet.processwire.com/pagearray-wirearray/change-tracking/a-resettrackchanges/).

This little tutorial should only be a starting point for your own coding.

  • Thanks 2

Share this post


Link to post
Share on other sites

Here is another working method, that only shows changes once after save. It is based on this post from Ryan.

//Compare before and after values
$pages->addHookAfter('saveReady', function($event)
{
    $page = $event->arguments('page');
    if(!in_array($page->template->name, array('template1', 'template2'))) return; //restrict it to these templates only
    $fields = array('field1','field2'); //check only changes on these fields        
    $this->wire('pages')->uncache($page);//uncache new page
    $oldPage = $this->wire('pages')->get($page->id);// get old page  
    $changedfieldsarray = array();
    foreach($fields as $field) {
     if (($oldPage->$field) != ($page->$field)) { //this is the comparison between before and after
      $changedfieldsarray[] = $page->fields->get($field, true)->label;
     }
    }
    if(count($changedfieldsarray) > 0) {
     $fieldnames = implode(", ", $changedfieldsarray);  
     $this->warning(sprintf(__("Following fields have been changed: %s"), $fieldnames )); 
    }      
});

It uses the uncache method to compare stored values from the DB and the new ones entered in the form. This informs the editor only once directly after form submission. If the editor push the save button once more and nothing was changed, the message will not be shown again. This is the main difference between this and the previous method.

Attention: This method uncaches the page and therefore all entered values will be cleared after pressing the submit button and no sucessfull saving of the page - so use this method with care.

  • Like 1
  • Thanks 1

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.