Page update hook before content is written?


i’ve a question:

is it possible to get the content of a page inside a hook before the new content will be overwrite the old one?
From the doc: "Hooks are being triggered after the event is called in the panel."
Some kind of “before update hook” would be great but this is not possible, right?


No, there is no before update hook. While the old page content is or should still available after the hook is triggered, the hook is always triggered after the event. What do you want to do?

Writing a “diff” of a text field to a hidden sub page to track changes.
I have to implement a approval/release process for a kirby site.

Since you always get the old and new page object, you can find out the differences without a problem, you don’t need a pre hook for that. I always wanted to extend my Kirby Logger plugin to save differences to a database, just haven’t got round to it yet.

kirby()->hook('', function ($page, $oldpage) {
 // your code

Ok, how can i access the old and new object inside the hook? (never done a hook before, sry)

EDIT: oh i see, thank you! :slight_smile:

Last week i tested the code and it works just fine… but now $page and $oldPage are always the same. No matter what i change in the panel. I’ve no idea how to debug or fix this…


What have you changed in the meantime?

Could you post your complete code for testing please?

Maybe… Hooks are not that easy to debug… i write debug text to a txt file (located in /panel after running)

kirby()->hook('', function($page, $oldPage = null) {

 $txxt = '.oOUPDATE_oldOo.'.var_export($page->content()->toArray(),true); $myfile = file_put_contents('looogs.txt', $txxt.PHP_EOL , FILE_APPEND | LOCK_EX);
 $txxt = '.oOUPDATE_newOo.'.var_export($oldPage->content()->toArray(),true); $myfile = file_put_contents('looogs.txt', $txxt.PHP_EOL , FILE_APPEND | LOCK_EX);

EDIT: it has something to do with the “global-field-definitions” and “Field groups”… i’m still hunting the bug.
EDIT: removed field groups… still the same bug…

Instead of writing to file, you can also use error_log() to write to the PHP error log file.

Apart from that, it works for me, so I guess it has something to do with your particular installation, either Kirby or plugin related. This is the code I’m using:

kirby()->hook('', function($page, $oldpage = null) {
  $old = $oldpage->content()->toArray();
  $new = $page->content()->toArray();

on the ZendServer here error_log was not working as expected so i used the file approach…anyway:
thx for the double check.
It seems it “just” want work for one module(blueprint)… hmmm

Could you paste a blueprint that does not work? Are you using this in conjunction with the modules plugin?

title: Module Hero
icon: cube
pages: false
files: true
  # The title is only used in the Panel for this module
    label: Module title
    type: text
  # The text is used in the module's snippet as $module->text()
    label: Text
    type: textarea
    label: Headline
    type: text
    label: Cover Image
    type: image
    label: minimale Höhe in Zeilen
    type: text
    label: Attach horizontal 
    type: select
    width: 1/2
      left: Left
      center: Center
      right: Right
    label: Attach vertical
    type: select
    width: 1/2
      top: Top
      center: Center
      bottom: Bottom  
    type: headline
      de: Freigabe
      en: Approval
    label: audit pending
    type: text #hidden
    default: ''
    label: auditedby
    type: text #hidden
    default: ''
    label: Geprüft
    type: audit
    text: This is just a test

“hheadline” was between “title” and “text” and the name was “headline”… but changes doesn’t help.


I’ll try to look into this tonight when I’m back home.

1 Like

I see you’re also using an image field.
Is the site multilingual?

Then you might have run into a nasty bug: $oldPage in hooks doesn't return previous version of the page

It’s already filed on github, and I’ve seen in it is in the next release milestone page (I really hope it gets fixed :smile:).

I deleted the content txt file and now, after refilling the page in the panel, it works!? Even with a field called “Headline”… thought it is a reserved word but it seems not.

@bvdputte: yes, image field and two languages… … . gnmpf

Alright, @bvdputte is right, we need not look any further, the image field is the villain in multi-lang setting. The next beta should be out soon…