Hi,
I want to implement simple pagination BUT filter the list before via twig templates.
This is a normal pagination setup:
{# Get the collection #}
{% set collection = page.collection() %}
{# Paginate collection #}
{% set limit = 1 %}
{% do paginate( collection, limit ) %}
{# Print paginated elements #}
{% for item in collection %}
<h5>{{ item.title }}</h5>
{% endfor %}
{# Pagination navigation '<< 1 2 3 >>' #}
{% if config.plugins.pagination.enabled and collection.params.pagination %}
{% include 'partials/pagination.html.twig' with {'base_url':page.url, 'pagination':collection.params.pagination} %}
{% endif %}
Now I want to add a special twig filter before:
{# Filter collection (currently does not filter anything, as always returns true. Used to demonstrate the bug) #}
{% set collection = collection|filter(x => true) %}
I will then get an error:
Argument 1 passed to Grav\Plugin\PaginationHelper::__construct() must be an instance of Grav\Common\Page\Collection, instance of CallbackFilterIterator given
Now luckly I found a workaround, but I am not really happy with it:
{# Filter collection (currently does not filter anything, as always returns true. Used to demonstrate the bug) #}
{% set collection_twigfilter = collection|filter(x => true) %}
{# Workaround to cast the filtered list back into a collection object to allow pagination #}
{% set collection = page.collection({'items': ''})%}
{% for collection_page in collection_twigfilter %}
{% do collection.addPage(collection_page) %}
{% endfor %}
Is there a way to avoid that? I expected to have some collection|filter(x => true)|cast(collection)
option?