Hi,
I have a structure that stores references to siblings
artistproject:
label: Project
type: select
options: query
query:
fetch: siblings
template: artistproject
When I now change the URL of one of the siblings, the stored reference will break. I am trying to fix this with the following hook:
kirby()->hook('panel.page.move', function($page, $oldPage) {
$oldUid = $oldPage->uid();
$newUid = $page->uid();
$artistPages = site()->find('artists')->children();
foreach($artistPages as $artistPage)
{
foreach($artistPage->children() as $artistProjectPage)
{
$rawContent = $artistProjectPage->content()->raw();
str_replace($oldUid, $newUid, $rawContent);
// and now?
$media->save($rawContent); // ? !?!?
}
}
});
The idea is to iterate trough the content of all relevant pages, replace the old reference and save the file. What am I missing?
Thank you!
That is a lot easier, all you have to do is update the field if the field contains the old value:
if($artistProjectPage->artistproject() == $oldUID) {
$artistProjectPage->update(array(
'artistproject' => $newUid,
));
}
https://getkirby.com/docs/cheatsheet/page/update
Thanks, that’s indeed a quite good answer. However, and I’m sorry for that, my question has some flaws.
I’ve written it, but didn’t include the yaml: I have a structure of page references, the full yaml is this:
more:
label: More
type: structure
style: table
fields:
artistproject:
label: Project
type: select
options: query
query:
fetch: siblings
template: artistproject
and also, I will have to update page references in other files, here is an example:
featuredprojecttop:
label: Featured Project Top
type: select
options: query
query:
page: artists
fetch: grandchildren
template: artistproject
That’s why I am looking for a general approach. I tried to get around dealing with the field names.
Then you probably have to use standard php methods to open, read and save the textfile.
Yep, okay. For anyone interested, it looks like this now:
kirby()->hook('panel.page.move', function($page, $oldPage) {
$oldUid = $oldPage->uid();
$newUid = $page->uid();
$artistPages = site()->find('artists')->children();
foreach($artistPages as $artistPage)
{
foreach($artistPage->children() as $artistProjectPage)
{
$rawContent = $artistProjectPage->content()->raw();
$newContent = str_replace($oldUid, $newUid, $rawContent);
$file = fopen($artistProjectPage->content()->root(), 'w');
fwrite($file, $newContent);
fclose($file);
}
}
});
Automatic updating of page references is something I would have suspected to be built in actually. Would be a cool feature! Thanks for the superfast support!