I’m creating pages and would like to run a Geo Plugin on the city, state for each new page. I would like to do this during page creation, but don’t know how to run a function at that time.
My creation script follows, inspired by this post. The last line that starts with 'coords'
is the one that breaks the script and gives an Invalid argument supplied for foreach()
error. Without the 'coords'
line it successfully creates pages, but doesn’t give me coordinates of course.
EDIT: more code added below
// get page entries from mysql database
$data= db::query('SELECT * FROM `my_table`');
// loop through each entry as a project
foreach($data as $project) {
try {
$newPage = page('projects')->children()->create(str::slug($project->ProjectTitle()), 'project', array(
'title' => $project->ProjectTitle(),
'institution' => $project->Institute1(),
'city' => $project->InstituteCity(),
'state' => $project->InstState(),
...
'coords' => geo::locate($project->InstituteCity(), $project->InstState())
// make new page visible, from https://forum.getkirby.com/t/kirby-api-make-page-visible-when-creating/3364/2?u=lukehatfield
))->sort( page('projects')->children()->visible()->count() +1);
} catch(Exception $e) {
echo $e->getMessage() . '<br>';
}
}