Hello there,
I hope you can help me with my question. I am looking for a way to display a limited set of randomly generated pages in the panel. In other words, I would like to randomly select a certain number of children of a parent page and then have that random selection be displayed in a panel field without it changing again in the future.
I know I can use the random() function on collections and pages to get a random selection. However, if I use that in the query of the pages field then it changes every time the panel page is called.
Question:Can I use the random() function in the default parameter of a pages field so that it is only called on page creation? Can the default field work with such a query?
No, default parameter does not (yet) support queries.
At the moment, all I can think of is to use a custom page.create:after hook and try to fill a pages field or a structure with random content once. Is that the best solution, or am I overlooking something obvious?
I have never used hooks in Kirby before—never had a reason to—and I am a little intimidated. If there is a simpler way to do this, it would be much appreciated.
Ideas are welcome. Cheers.
Edit:
The motivation for this is that I have a set of pages that contain quiz questions. Every contestant is supposed to get their own random selection from the overall question pool. I want them to see their selection of question on the front page, but I want to “remember” which question they answered in their profile panel page.
So, this works as a rough attempt, but it is by no means finished and I am hung up on the difficult bit of using a hook to set the default values.
questions:
type: structure
label: Questions
help: These questions were randomly selected.
default:
- question: page://szuomtcdwzistlyg
link: ../@/page/szuomtcdwzistlyg
- question: page://szuomtcdwzistlyg
link: ../@/page/szuomtcdwzistlyg
fields:
question:
type: link
link:
type: url
label: Link to the question
This works and gives me a structure field with the values pre set. However, I read that the page.create:before hook does not let you change the content of the page. If I were to use the page.create:after hooks then I would not have to work with default values, right?
Not sure, I really understand the intended workflow. Let me see if I understand correctly.
You are trying to create a page in the Panel for a given user which serves as the basic for the quiz pages this user gets to see?
Where are the results stored?
A hook would be the right way to go in this case. Inside a page.create:after (!) hook you would create a random set of pages out of the full set, and store these pages in a pages field inside the page.
Somethings like
'hooks' => [
'page.create:after' => function ($page) {
if ($page->intendedTemplate()->name() === 'whatever') {
// get 5 random quiz pages
$quizPages = page('quizquestions')->children()->listed()->shuffle()->limit(5);
$page->update([
'selectedQuizPages' => Data::encode($quizPages->pluck('UUID', ','), 'yaml'),
]);
}
},
],
Adapt as needed regarding the template, page name etc.