Override a custom page method with a page model method?

I have a custom page method, that is the same but for one template.
So I tried to override the custom method in the page model by creating a method with the same name.
This does not seem to work though.

Is there an easy way to make this work?

Indeed this is possible:

https://getkirby.com/docs/guide/templates/page-models#overriding-the-page-class

Could you post your codes that tried to overriding method?

That describes how default Page methods are overwritten. Maybe custom page methods are different in that? I’m really no expert in these waters…

My code:

Kirby::plugin('myname/page-methods', [
    'pageMethods' => [
        'getChildrenJSON' => function() {
           // stuff
        }
    ]
]);

and

class ArtistPage extends Page {
  public function getChildrenJSON() {
    // different stuff
  }
}

Be aware that - contrary to page models - custom page methods cannot override default page methods.

You can read following section to get more information about difference:

You are right of course - I cannot override a default page method with a custom method.
But I actually want to override a custom page method through a page model. And since I can override a default page method with page models, I was hoping the same was true for the custom page method.

page model > default page method = true
page model > custom page method = ?

Now everything clear.
Following sample already works for me.

Kirby::plugin('myname/page-methods', [
    'pageMethods' => [
        'getChildrenJSON' => function() {
           return "Page Method";
        }
    ]
]);

class NotePage extends Page {
    public function getChildrenJSON() {
        return "Page Model";
    }
}

echo $page->getChildrenJSON(); // Output: Page Model
1 Like

Alright, just what I wanted!
Then I at least know I had the right idea. I probably made some stupid little mistake somewhere else.

Thanks for the troubles!

(Just to confirm: I did, of course, have a dumb mistake somewhere else. Overwriting as shown above works just fine.)