Prev() and next() with custom collection

Hey guys,

as it is written in this post, the functions _next() and _prev() used to do what I am trying to accomplish :smiley: Also it seems I am not the only one – Previous/next article within specific tags?.

So here’s the thing: I want to use next() and prev() functions, together with hasNext()/hasPrev() and so on – but in a custom collection, for example just the images of my folder. But with next() and prev() I always get all files/siblings from the parent collection, without filtering something out. For the moment I hack the code to make those functions not protected anymore, but of course it would be great if there would be a solution directly in the core, without modifying it…

If there’s not an existing feature I can use in this case, I’ll post this as an suggestion, but I wanted to make sure I haven’t failed to see something obvious :wink: Or has anybody another idea how to accomplish this in an easy way?

Thank you!

Ha, downside! Those functions are only made for pages, not for files… So more than ever I’m searching for a solution without modifying the core to much :smile:

Don’t know if I get you right, but you can do this to show only one (or more) images per page (you can filter images as well in this loop, if you want):

<?php $images = $page->images->paginate(1);
  $pagination = $images->pagination();
  foreach($images as $image) :?>
    <img src="<?= $image->url() ?>">
  <?php endforeach ?>

<?php if($pagination->hasPrevPage()): ?>
<a href="<?= $pagination->prevPageUrl() ?>">
  prev (<?=  $pagination->prevPage() ?>)
<?php endif ?>

<?php if($pagination->hasNextPage()): ?>
<a href="<?= $pagination->nextPageUrl() ?>">
  next (<?= $pagination->nextPage() ?>)
<?php endif ?>

Hm, no that’s not quite my use case. I have different pages (projects), each with multiple files insight. Sometimes only images, sometimes only videos, sometimes both and documents, … . I open the “file view” by appending a parameter to the page url (file), so if param('file') returns a value (and a file is found), the file view is shown. There I have a simple arrow-navigation for prev/next file – but those should only navigate within the same type of file, so if an image is open, just navigate between the project’s images, and ignore videos and documents. So I need some kind of backwards mechanism – select all files with the same file type like the open one.

My solution for the moment was indeed just copy-pasting the _next/_prev-functions to kirby/core/file.php, and adjusting the first parameter to take only Files instead of Children. It works like a charm now, but unfortunately it’s only possible with modifying the core… is there an obvious reason I’m missing why the _next/_prev-functions are a) only available for a page and not for files, and why they are b) protected and not “officially” supported? I mean, even the usual next and prev function of a page just point to those functions… :wink: Wouldn’t it be the best if they would live inside an Obj?

With Kirby 2.3 you can now achieve the same with custom page, file etc. methods, just tested this for a project and it works well.

I have no idea why those methods are protected, maybe because they are only useful if you have a method to keep your original collection by either using an URL parameter, or storing those values in the session, or using cookies …

Thanks for keeping this up to date! Awesome service, as always :wink: