How to get current page object in panel section plugin?

Hi,

I am following this recipe My first Panel section | Kirby CMS
in order to create custom section which would display in a table content from json

The json file is populated from frontend users input and saved by the name of $page->uid()

Now in the index.php of my plug-in, respectively the links.php in the above recipe, I’d like to have access to the current page object.

nothing I tried worked

$kirby = Kirby::instance();
$page = $kirby->site()->pages()->current() ;

result:
$page is null

$this->model()

result:

Using $this when not in object context

How can I get the current page object in custom panel section plugin?
i.e.
here My first Panel section | Kirby CMS I’d like to get the $page->uid() in order to load the proper json file like this:

$jsonFile=$kirby->root('site').DIRECTORY_SEPARATOR.'db'.DIRECTORY_SEPARATOR.$page->uid().'.json';
$data =\json_decode(\file_get_contents($jsonFile));

At no place I could get the current page object until I tried to do so in computed property: :confused:

return [
    'props' => [
       ...
    ],
    'computed' => [
        'data' => function () {
            $kirby = Kirby::instance();
            $jsonFile= $kirby->root('site').DIRECTORY_SEPARATOR.'db'.DIRECTORY_SEPARATOR.$this->model()->uid().'.json';

            return \file_exists($jsonFile) ? \json_decode(\file_get_contents($jsonFile)) : [];
        
        }
    ]
];

:see_no_evil:

Ok, you beat me to it, great that you figured it yourself :grinning:

Could you please explain briefly why is that only within computed property we do have access to current page object? :confused:

Any properties that you want to use in the vue part of your sections you have to pass to them, as props, computed props or methods. props are static properties (think of them as the parts you have in your blueprint, like the label etc), computed are also properties, but you can do all sorts of manipulation/calculation in there, like for example do something with the β€˜props’.

These PHP files are loaded in the Component class and the props etc. applied there.

1 Like