Listen to certain page changes in hooks

I have the following use case: the administration for a sports clubs’ event is based on kirby. Teams can sign up and also login to edit their data (custom front end, no panel access), the data is saved as single pages. The admin can also edit the data through the panel. Now, I need to inform another, third person, if some special data (dates for training) has changed, and send an email with the new data if this happens. Currently, I don’t really see a possibility for doing that via panel hooks – is this right? Maybe some of you have an additional method or idea for me?

So you need to inform this third person of both the changes that are done via the panel by the admin and the changes by team members via the custom form? For the first scenario, you can use a panel hook, for the second, you could handle this via your form processing script after the page is successfully updated?

Exactly. The problem is that I only want to notify if there are changes to certain fields, not after every save. The notification is only about those fields, so I need a way inside the hook of comparing the old vs. the new value. The changes via the custom form a not a problem at all here, just the hook things :wink:

The problem is that the old value is not stored anywhere, so there is nothing to compare it to. The only way around that would be to create hidden fields that get the same content as the fields you want to watch. These fields then only get updated if their content is different from the real fields. So they always contain the old status. Or some json files as backup.

Edit: This is a very basic outline, but something like that should work:

kirby()->hook('panel.page.update', function($page) {
  $field = $page->some_field()->value();
  if($page->content()->hasField('history')) {
    $fieldHistory = $page->history()->value();
  } else {
    $fieldHistory = "";
  }
  if($field !== $fieldHistory) {
    $page->update(array(
      'history'   => $field,
    ));
    mail('mail@example.com', 'New Data', $field);
  }
});
2 Likes

That looks like a nice solution for the moment. Thank you very much, I think I can go with this approach :slight_smile: