Adding a slideshow image counter — 1 / 4, 2 / 4, 3 / 4, 4 / 4 — to gallery block

Hi all, I am trying to customize the gallery block to make it into a slideshow. The one problem I am running into is adding an image counter into the nav.

I’m getting this error in the front end, any tips?
Block error: "syntax error, unexpected variable "$default", expecting ")"" in block type: "gallery"

One note is that when I apply the counter to a template php file, it doesn’t give this error, seems to be something specific to the block setup?

File path is:
/site/snippets/blocks/gallery.php

<?php
/** @var \Kirby\Cms\Block $block */
$caption = $block->caption();
$images  = $block->images();
$total   = $block->images()->toFiles()->count()->toInt(int $default = 0)->int();
?>
                <div class="image-block">
                  <?php 
                  $total = $images->count();
                  foreach ($block->images()->toFiles() as $image): ?>
                    <div class="mySlides">
                      <img class="slideshow" src="<?= $image->url() ?>" alt=""></a> 
                    </div>
                  <?php endforeach ?>

                    <div class="nav">
                        <div class="buttons">
                          <button onclick="plusDivs(-1)">&larr;</button>
                          <?= $images->indexOf($image) + 1 . "/" . $total;?>
                          <button onclick="plusDivs(1)">&rarr;</button>
                        </div>
                        <?php if ($caption->isNotEmpty()): ?> 
                            <?= $caption ?>
                        <?php endif ?>
                    </div>

                </div>

This will never work, count() already returns an integer, so calling a field method like toInt()doesn’t make any sense.

This line won’t work either. $images is defined as field, and you cannot count a field.

So you have to define

$images  = $block->images()->toFiles();
$total = $images->count();

Then do not define your images angain in the loop, but use the $images variable.

foreach ($images as $image): ?>

As always, thank you! that worked perfectly.