Call page model in panel field

i am developing a new plugin and would like to make use of page model functions within my panel field. but this does not work since models are not loaded in panel.

just calling `kirby()->models()’ and getting the page again like described here did not work within the field code.

i tried a reflection cast but it fails

ArgumentCountError: Too few arguments to function 
PageAbstract::__construct(), 0 passed in ... on line 8 and exactly 2 expected

calling kirby()->models() in field template causes page object class to change from Kirby\Panel\Models\Page to Page. Which would be ok since i use the var only within the plugin code. but I expected it to be the models class acctually MytemplatePage.

But calling page model functions still fails. any other way to ‘attach’ the pagemodel class to a current page object?

edited:
i now have a proper model class object but i am gettin this error…

Undefined variable: _client

but there is no var client in mine or all kirby/panel code, right? what am i missing?

edited: oh well. it was my var after all. so nevermind.

maybe the master of plugins @jenstornell might find this interesting…

// inside field code...

$page = $this->page();
$pageWithModel = null;
if(count(Page::$models) == 0) {
	// load only once even if field is called more often
	kirby()->models();
}
// if model exists
$cname = str::lower($page->template().'Page');
if(a::get(Page::$models, $page->template()) == $cname) {
	$pageWithModel = new $cname($page->parent(), $page->dirname());
}
// now do something with $pageWithModel that can not be done with $page
1 Like

Looks interesting. :slight_smile:

I added it as an issue here: https://github.com/jenstornell/kirby-secrets/issues/16

1 Like