Hi there! I am trying to generate an index inside a hook and save it to a read-only field. The plan is to collect all pages by category and find the index of the current page. Tested it in different ways—apparently the index always returns nothing. Does indexOf() not work inside panel hooks? My function inside the config looks like that:
$setIndex = function($page) {
if ($page->intendedTemplate() == 'product') {
$getCategory = $page->category();
$getCategoryElements = $page->parent()->children()->filterBy('category', $getCategory)->sortBy('date', 'asc');
$getCategoryIndex = $getCategoryElements->indexOf($page);
$setCategoryIndex = sprintf('%04d',$getCategoryIndex);
$setCategory = substr($getCategory, 0, 2);
$setCategory = strtoupper($setCategory);
try {
$page->update(array(
'index' => $setCategory . $setCategoryIndex
));
}
catch(Exception $e) {
echo $e->getMessage();
}
}
};
kirby()->hook('panel.page.*', $setIndex );