Simple changelog hook for Kirby

As already requested by Discord here is our small solution for a very simple and easy changelog for pages. Thanks to @texnixe for help with the function a few months ago. The hook can be inserted into the config or into a plugin.

'hooks' => [
  'page.update:after' => function($page) {
    $user = $this->user()->username();
    function addToStructure($page, $field, $data = array()) {
      $fieldData = page($page)->$field()->yaml();
      $fieldData[] = $data;
      $fieldData = yaml::encode($fieldData);
      try {
        page($page)->update(array($field => $fieldData));
        return true;
      } catch(Exception $e) {
        return $e->getMessage();
      }
    }
    addToStructure($page, 'changelog',[
      'changelog_date' => date('d.m.Y'),
      'changelog_time' => date('G:i:s'),
      'changelog_user' => $user
    ]);
  }
]

This can then be used as a blueprint.

changelog:
  type: structure
  label: Changelog
  limit: 1000
  disabled: true
  fields:
    changelog_date:
      label: Date
      type: text
    changelog_time:
      label: Time
      type: text
    changelog_user:
      label: User
      type: text
3 Likes

Thank you, Oliver!

I am glad that I could help and could contribute a small part :slight_smile:

1 Like

Iā€™m pretty insterested by your solution, looking for that type of changelog but get an error message saying

Exception: Error
Call to a member function changelog() on null

Does anyone have a similar issue with this code ?

The hook variable page is wrong, should be $newPage

1 Like

Has this Hook always been like that with new and old? Because in the old instance of me the code still works.

No, that was a change that happened in 3.4. Variables are since named, but can be used in any order.