indexOf() inside panel hook

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 );

Got it. I had to get the index of the relative URL. Is this the normal behaviour? Worked in another function like my previous solution.

$relativeUrl = $page->parent()->uid() . '/' . $page->uid();
[…]
$getCategoryIndex = $getCategoryElements->indexOf($relativeUrl);
$setCategoryIndex = sprintf('%04d',$getCategoryIndex + 1);

That’s what I was wondering as well, in a template/controller etc, you can pass the page object as parameter and that is how it is used within the Kirby source code as well.

Edit: I think this happens because $page in this context is not a Kirby page object, but a Panel Page object.

Makes sense! Thank you for the explanation!