Hi,
I have a collection of pages, which I have generated in a controller. I am using pagination on this collection.
I now need to shuffle the pages, so they appear in a random order, while also maintaining the pagination. I have tried shuffle()
, but this re-shuffles on each pagination so isn’t working as I need. I need to shuffle all the pages, and then have the pagination step through that shuffled collection without re-shuffling.
I found a plugin on here using a seed, but it doesn’t seem to be working for me. If anyone has any thoughts that would be great. My controller code is below for reference (with my current shuffle()
near the end):
/site/controllers/art-and-artists.php
<?php
return function($page,$site,$kirby,$pages) {
$shared = $kirby->controller('site' , compact('page', 'pages', 'site', 'kirby'));
// fetch the basic set of pages
$artworks = $site->find('art-and-artists')->children()->sortBy('lastName','asc')->children()->published();
// fetch the set of artwork categories used in all artworks
$categories = $artworks->pluck('categories', ',', true);
// this is the categories defined for use on the page
$pageCategories = $page->artworkCategories()->toStructure();
$categoriesForFilter = [];
foreach($pageCategories as $category):
$slug = $category->category()->slug();
if(A::has($categories, $slug)):
$categoriesForFilter[] = $category->category();
endif;
endforeach;
// fetch the artists
$artists = $site->find('art-and-artists')->children()->sortBy('lastName','asc')->published();
// now for some filtering
$artistFilter = get('artist');
$categoryFilter = get('category');
$priceFilter = get('price');
$artworks = $artworks
->when($categoryFilter, function ($categoryFilter) {
return $this->filterBy('categories', $categoryFilter, ',');
})
->when($priceFilter, function ($priceFilter) {
return $this->filterBy('priceBracket', $priceFilter, '|');
})
->when($artistFilter, function($artistFilter) {
return $this->filter(fn ($child) => $child->parent()->slug() == $artistFilter);
});
// apply pagination
$artworks = $artworks->shuffle()->paginate(48);
$pagination = $artworks->pagination();
return A::merge($shared , compact('artworks','artists','categoriesForFilter','pagination'));
};
?>