hook('panel.page.*', function($page, $oldPage = null) { // If the page is a release if($page->intendedTemplate() == 'release') { // Get all artists $artists = site()->index()->filterBy('intendedTemplate', 'artist'); // For each artist foreach ($artists as $key => $a) { $index = 0; // For each release of the artist foreach ($a->children()->published() as $key => $p) { // Get all tracks of the release $tracks = $p->tracklist()->toStructure(); // For each track foreach ($tracks as $key => $t) { // If the track has and audio file if($file = $t->audioFile()->toFile()) { // Update the track index $file->update(['trackIndex' => $index]); $index++; } } } } } });