So, I briefly tested this in a new Starterkit. Now here some basic steps:
-
Create a new file in plugins, e.g. hooks.php
-
Register a hook like this:
<?php kirby()->hook('panel.page.create', function($page) { // your hook code });
Now, every time any page is created in the panel, the hook is triggered and the code inside is executed.
-
For test puposes, I created a structure field in the projects blueprint like this
```text projects: label: Projects type: structure fields: projecttitle: label: Project Title type: text projectyear: label: Project Year type: text
-
Next, in the hook.php file I added the following code:
<?php
kirby()->hook('panel.page.create', function($page) {
//check if page is child of projects
if($page->isChildOf(page('projects'))) {
$newPage = $page;
}
// this is the page which has the structure field type
$projectpage = site()->find('projects');
// get existing entries in an array
$projectarray = $projectpage->projects()->yaml();
// add a new entry to the array
$projectarray[] = ['projecttitle' => $newPage->title(), 'projectyear' => $newPage->year()];
// update the structure field type
page('projects')->update([
'projects' => yaml::encode($projectarray)
]);
});
So, what this does, every time a page is created, the new page is added to the structure field (code based on a suggestion in this post Add method to append to structure field. Guess, we can now use the new toStructure() method here as well and add to the collection.
Note: This is just to show the basic functionality.
You would have to create a second hook with the same code but panel.page.update
instead of panel.page.create
to update the field when the page is updated as well.
There are some downsides to this basic code, in the first step (when the page is created) nothing but the title will be added to the structure field. Also, every time you update (with the second hook), a new entry will be created, so the code needs to be refined to prevent that.
But in general, the above works, which is a starting point, I hope …