Hi,
I am using a hook to make some changes to a page, and am looping through some data as part of this. This is the code I have:
'page.create:after' => function ($page) {
if ($page->template()->name() === 'school') {
$schoolType = $page->schoolType();
$blockPages = page($schoolType)->children();
foreach ($blockPages as $blockPage) {
$blockName = $blockPage->slug();
foreach ($blockPage->children() as $sectionPage) {
$sectionName = $sectionPage->slug();
$schoolKeyTasksField = site()->getSchoolKeyTasksFieldName($blockName, $sectionName);
$pageKeyTasks = $sectionPage->keyTasks()->yaml();
$schoolKeyTasks = [];
foreach ($pageKeyTasks as $keyTask) {
$schoolKeyTask = $keyTask;
$schoolKeyTask['questionautoid'] = $schoolKeyTask['autoid'];
unset($schoolKeyTask['autoid']);
$schoolKeyTasks []= $schoolKeyTask;
}
$page->save([
$schoolKeyTasksField => Data::encode($schoolKeyTasks, "yaml")
]);
}
}
}
},
When the hook runs, only the last run of the foreach loop is actually saved to the page. I can’t work out why this would be - can I only use the save once in a hook?