Pagination configuration


Following the docs for pagination, I had a controller for page similar as the docs one with the code ;

$articles = $page->children()->visible()->flip()->paginate(9);

it works well but I would like to go a step further.
In the page blueprint, I created a field “pagination” where I specified the number of articles to display by page (pagination (6 or 10 or …)) instead of the number 9 plugged in the controller.

I would like to know how to implement within the controller a way to introduce the parameter $page->pagination to differentiate pagination depending on the figure provided in the blueprint.
I guess it could be done through array or something like that, but my skills in php are too limited to implement it correctly (if relevant).

In addition I saw somewhere in the forum the possibility to config directly in the config.php file through

c::set(‘pagination-books’, 8): number of children per page (folder books)
c::set(‘pagination-news’, 10);: number of children per page (folder news)

I don’t know how to implement either one solution or the other and which is the most efficient/clean…if workable.

Thank you

What would be the advantage of defining the number of pages of the pagination in the blueprint or in the config file over putting it in the controller?

If you want to put in the config file, you can set it like this:

c::set('pagination-blog', 9);

Then you would get it in your controller like this:

// get the number from the config file, if not set use default:
$limit = c::get('pagination-blog', 6);
$articles = $page->children()->visible()->flip()->paginate($limit);

If you want to make it dynamic, so that the user can define how many pages to have on one page, define a new field in your controller:

    label: No. of pages per page
    type: number

Then in your controller:

$limit = $page->pagination_number()->int();
$articles = $page->children()->visible()->flip()->paginate($limit);

Edit: if you really want to set the number in the blueprint directly, you would have to read the blueprint, but I don’t think that makes sense.

You are right.
First, I intend to do it in the controller but I don’t know how to do it (array ?).
Appreciate any help for that way.

Do you still have a question regarding the above solutions?

Yes, I do
As far as I understood, you said that it would be better to do it in the controller (instead of going through blueprint and config.php).
I would like to follow your advise (without set a number in the blueprint) but still don’t know how to do it

Yes, see example above, you define a field in the blueprint. Then the user can set a number in the panel. You then get the number in your controller from the content file.

You can even change the code in the controller a bit to check if the number is set and use a default if not:

// get the number from the content file if the field is not empty
if($page->pagination_number()->isNotEmpty()) {
  $limit = $page->pagination_number()->int();
// otherwise use a default number
} else {
  $limit = 9;
// set pagination number to this variable
$articles = $page->children()->visible()->flip()->paginate($limit);

I tried to implement your solution and I got the following message:

Notice: Object of class Field could not be converted to int in D:\UwAmp\www\my-app\kirby\toolkit\lib\pagination.php on line 64

Then try to change the object to integer:

$limit = $page->pagination_number()->int();

It works perfectly well
Thanks a lot