I am updating from Kirby 2 to Kirby 3.
I have this code to programaticaly create pages from a local json file (cobbled together from suggestions from the forum, because my php skills are weak). Worked great in Kirby 2; it has stopped working in Kirby3.
The debugger says that the “Invalid argument supplied for foreach()”. I’ve been using an old Json file that was set up correctly, so there’s no problem there.
return function($site, $pages, $page) {
$alert = null;
$json = [];
if($page->file()) {
$src = page()->file();
if(r::is('post') && get('good')) {
$content = f::read($src);
$json = json_decode($content, true);
foreach($json['entrants'] as $entrant){
$data = array(
'idno' => $entrant['idno'],
'firstname' => $entrant['firstname'],
'lastname' => $entrant['lastname']
);
$uid = $entrant['lastname'] . '-' . $entrant['firstname'] . '-' . $entrant['idno'];
$template = 'gallery-project';
try {
$folder = page()->parent();
$newPage = $folder->children()->create($uid, $template, $data);
$success = 'The new pages have been created';
echo 'The new page for '. $uid .' has been created<br>';
} catch(Exception $e) {
echo 'Page creation has failed: ' . $e->getMessage();
}
};
}
}
return compact('alert', 'src','folder','newPage', 'success');
};