Page model doesn't working inside file hook

I have a file hook that does the following:

  • Renames a file based on it’s parent page attributes
  • Adds ID3 tags based on it’s parent page attributes
  • Reads the filesize and length, then updates the page
  • Uploads the file to S3
  • Deletes it locally

All the hard stuff works fine. The problem is that when I call a page method that’s been defined in a model, it doesn’t work inside the hook (although it’s fine on the website).

// Model

public function author()
    $slug = 'preacher/'.parent::author();

    return site()->pages()->find($slug)->fullName();

When I call $file->page()->author() inside the hook I only get the slug not the formatted name.

Page models are not loaded and applied in the Panel automatically to avoid interference with the actual content files.

You could try calling kirby()->models() in your hook before accessing the page to manually load them.

Tried that but it didn’t work, only got the slug.

I do understand the reasoning that page models are not loaded in the panel (and thus in hooks). The kirby()->models() does not work for me either unfortunately. Any other ideas on how to get the page as instance of the correct page model inside a hook?

As a sidenote: It might also be worth changing the docs for page models, where it currently says

This page model will be loaded every time Kirby encounters a page of the given type (so project in this example), whether it is in a template, a snippet or anywhere else.

Just realized, that kirby()->models() does work for me after a small change.

Inside the hook I was trying to access the page model of the currently edited page. Loading the models inside the hook (using kirby()->models()) didn’t have any effect on the page itself, but when requesting the page again I got the correct page model.

I’ve used the following code inside the hook:

kirby()->models(); $correctPageModel = page($page->uid());


what interference could that be? i just did that and want to avoid major bugs in panel…

Just loading the models with kirby()->models() and then creating an instance for yourself shouldn’t cause any issues.

It’s just that if that instance somehow becomes part of Kirby’s internal object structure, it can cause issues. For example a field could read the content from the instance to display it in the Panel. If the return value of the model doesn’t actually represent the content in the content file, you’d then overwrite the content file with something else on save.

i see. thanks for explaining.