Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pagination does not disappear if no pagination is required. #50

Open
NicoHood opened this issue Sep 5, 2020 · 1 comment
Open

Pagination does not disappear if no pagination is required. #50

NicoHood opened this issue Sep 5, 2020 · 1 comment

Comments

@NicoHood
Copy link

NicoHood commented Sep 5, 2020

I found multiple issues with the current pagination plugin. I have some solutions, but you must decide which one will fit best.

My goals:

  • Show a pagination menu if there are items to paginate
  • Hide this menu if there are not enough posts (post count is smaller than pagination limit)

Currently the twig template checks, if the pagination length is greater than 1:

Issue A

The Item index is wrong (one too high). If you have 10 Items, limit set to 5, you'd expect to have 2 paginations. The length will output 3!

for ($x=1; $x <= $this->page_count; $x++) {
if ($x === 1) {
$this->items[$x] = new PaginationPage($x, '');
} else {
$this->items[$x] = new PaginationPage($x, '/page' . $config->get('system.param_sep') . $x);
}
}
}

Fix

We need to place the items to x-1, because the counting starts at 1, not 0.

for ($x=1; $x <= $this->page_count; $x++) {
    if ($x === 1) {
        $this->items[$x-1] = new PaginationPage($x, '');
    } else {
        $this->items[$x-1] = new PaginationPage($x, '/page' . $config->get('system.param_sep') . $x);
    }
}

Edit: This fix break the hasPrev() functions. This can be solved somehow for sure, just noting it here, so you do not forget to change that as well!

Issue B

Now that we have fixed the length, we would expect the pagination menu to disappear, if the limit is not reached. But that does not happen, because now pagination|length will return 4. Why does it happen? Thatswhy:

public function paginateCollection($collection, $limit, $ignore_param_array = [])
{
$collection->setParams(['pagination' => 'true']);
$collection->setParams(['limit' => $limit]);
$collection->setParams(['ignore_params' => $ignore_param_array]);
if ($collection->count() > $limit) {
$this->pagination = new PaginationHelper($collection);
$collection->setParams(['pagination' => $this->pagination]);

pagination is set to true, which is string of length 4.

Fix

We can fix that, by checking the type inside the twig template:

{% if (pagination|gettype == 'object') and pagination|length > 1 %}

However I am not sure, if that is the best way to do. We could also use an empty element, but that caused the whole template to not render at all. But this is an issue you should be able to fix, as I am new to all the code, and you could think of a better solution and you know if they string true is required or not.

Hope that explanation is detailed enough. :-)

@NicoHood
Copy link
Author

NicoHood commented Sep 5, 2020

Related: #31

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant