From 437b1243b9f7628926a0e8b04850b914c485fb5b Mon Sep 17 00:00:00 2001 From: Massimiliano Arione Date: Mon, 30 Sep 2024 09:16:09 +0200 Subject: [PATCH] add new option to enhance first page link --- README.md | 12 ++++++-- config/paginator.xml | 2 ++ src/DependencyInjection/Configuration.php | 3 ++ .../KnpPaginatorExtension.php | 2 ++ src/Helper/Processor.php | 12 +++----- src/Pagination/SlidingPagination.php | 6 +--- .../SlidingPaginationSubscriber.php | 6 +--- src/Templating/PaginationHelper.php | 12 +++----- src/Twig/Extension/PaginationExtension.php | 2 ++ src/Twig/Extension/PaginationRuntime.php | 28 +++++++++++++++---- .../Pagination/bulma_pagination.html.twig | 16 +++++------ .../foundation_v5_pagination.html.twig | 14 +++++----- .../foundation_v6_pagination.html.twig | 14 +++++----- .../materialize_pagination.html.twig | 10 +++---- templates/Pagination/rel_links.html.twig | 4 +-- .../semantic_ui_pagination.html.twig | 10 +++---- templates/Pagination/sliding.html.twig | 10 +++---- .../tailwindcss_pagination.html.twig | 10 +++---- .../twitter_bootstrap_v4_pagination.html.twig | 14 +++++----- .../Pagination/uikit_v3_pagination.html.twig | 14 +++++----- .../DependencyInjection/ConfigurationTest.php | 2 ++ 21 files changed, 112 insertions(+), 91 deletions(-) diff --git a/README.md b/README.md index 29e5ed54..2b28dfd6 100644 --- a/README.md +++ b/README.md @@ -22,7 +22,7 @@ chapter of the documentation. ## Requirements: -- Knp Pager component `>=2.0`. +- Knp Pager component `>=4.4`. - KnpPaginatorBundle's master is compatible with Symfony `>=6.4` versions. - Twig `>=3.0` version is required if you use the Twig templating engine. @@ -71,12 +71,14 @@ public function registerBundles() ### Configuration example -You can configure default query parameter names and templates +You can configure default query parameter names and templates, and a few other options: #### YAML: ```yaml knp_paginator: + convert_exception: false # throw a 404 exception when an invalid page is requested page_range: 5 # number of links shown in the pagination menu (e.g: you have 10 pages, a page_range of 3, on the 5th page you'll see links to page 4, 5, 6) + remove_page_1_link: false # remove the page query parameter from the first page link default_options: page_name: page # page query parameter name sort_field_name: sort # sort field query parameter name @@ -84,6 +86,8 @@ knp_paginator: distinct: true # ensure distinct results, useful when ORM queries are using GROUP BY statements filter_field_name: filterField # filter field query parameter name filter_value_name: filterValue # filter value query parameter name + page_out_of_range: ignore # ignore, fix, or throwException when the page is out of range + default_limit: 10 # default number of items per page template: pagination: '@KnpPaginator/Pagination/sliding.html.twig' # sliding pagination controls template rel_links: '@KnpPaginator/Pagination/rel_links.html.twig' # tags template @@ -101,7 +105,9 @@ use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigura return static function (ContainerConfigurator $configurator): void { $configurator->extension('knp_paginator', [ + 'convert_exception' => false, // throw a 404 exception when an invalid page is requested 'page_range' => 5, // number of links shown in the pagination menu (e.g: you have 10 pages, a page_range of 3, on the 5th page you'll see links + 'remove_page_1_link' => false, // remove the page query parameter from the first page link 'default_options' => [ 'page_name' => 'page', // page query parameter name 'sort_field_name' => 'sort', // sort field query parameter name @@ -109,6 +115,8 @@ return static function (ContainerConfigurator $configurator): void 'distinct' => true, // ensure distinct results, useful when ORM queries are using GROUP BY statements 'filter_field_name' => 'filterField', // filter field query parameter name 'filter_value_name' => 'filterValue' // filter value query parameter name + 'page_out_of_range' => 'ignore', // ignore, fix, or throwException when the page is out of range + 'default_limit' => 10 // default number of items per page ], 'template' => [ 'pagination' => '@KnpPaginator/Pagination/sliding.html.twig', // sliding pagination controls template diff --git a/config/paginator.xml b/config/paginator.xml index 3bdc57e5..2b37e4bf 100644 --- a/config/paginator.xml +++ b/config/paginator.xml @@ -56,6 +56,8 @@ + %knp_paginator.page_name% + %knp_paginator.remove_page_1_link% diff --git a/src/DependencyInjection/Configuration.php b/src/DependencyInjection/Configuration.php index 81c5d9d8..3769d587 100644 --- a/src/DependencyInjection/Configuration.php +++ b/src/DependencyInjection/Configuration.php @@ -55,6 +55,9 @@ public function getConfigTreeBuilder(): TreeBuilder ->booleanNode('convert_exception') ->defaultFalse() ->end() + ->booleanNode('remove_page_1_link') + ->defaultFalse() + ->end() ->end() ; diff --git a/src/DependencyInjection/KnpPaginatorExtension.php b/src/DependencyInjection/KnpPaginatorExtension.php index 5a1b6296..7313ae93 100644 --- a/src/DependencyInjection/KnpPaginatorExtension.php +++ b/src/DependencyInjection/KnpPaginatorExtension.php @@ -41,6 +41,8 @@ public function load(array $configs, ContainerBuilder $container): void $container->setParameter('knp_paginator.template.sortable', $config['template']['sortable']); $container->setParameter('knp_paginator.page_range', $config['page_range']); $container->setParameter('knp_paginator.page_limit', $config['page_limit']); + $container->setParameter('knp_paginator.page_name', $config['default_options']['page_name']); + $container->setParameter('knp_paginator.remove_page_1_link', $config['remove_page_1_link']); $paginatorDef = $container->getDefinition('knp_paginator'); $paginatorDef->addMethodCall('setDefaultPaginatorOptions', [[ diff --git a/src/Helper/Processor.php b/src/Helper/Processor.php index b5f1ba93..bac7ffc7 100644 --- a/src/Helper/Processor.php +++ b/src/Helper/Processor.php @@ -15,14 +15,10 @@ */ final class Processor { - private UrlGeneratorInterface $router; - - private TranslatorInterface $translator; - - public function __construct(UrlGeneratorInterface $router, TranslatorInterface $translator) - { - $this->router = $router; - $this->translator = $translator; + public function __construct( + private readonly UrlGeneratorInterface $router, + private readonly TranslatorInterface $translator + ) { } /** diff --git a/src/Pagination/SlidingPagination.php b/src/Pagination/SlidingPagination.php index 2c6b8b41..90421629 100644 --- a/src/Pagination/SlidingPagination.php +++ b/src/Pagination/SlidingPagination.php @@ -16,9 +16,6 @@ final class SlidingPagination extends AbstractPagination implements SlidingPagin { private ?string $route = null; - /** @var array */ - private array $params; - private int $pageRange = 5; private ?int $pageLimit = null; @@ -34,9 +31,8 @@ final class SlidingPagination extends AbstractPagination implements SlidingPagin /** * @param array $params */ - public function __construct(array $params) + public function __construct(private array $params) { - $this->params = $params; } public function setUsedRoute(?string $route): void diff --git a/src/Subscriber/SlidingPaginationSubscriber.php b/src/Subscriber/SlidingPaginationSubscriber.php index c94d19fa..6738b69b 100644 --- a/src/Subscriber/SlidingPaginationSubscriber.php +++ b/src/Subscriber/SlidingPaginationSubscriber.php @@ -15,15 +15,11 @@ final class SlidingPaginationSubscriber implements EventSubscriberInterface /** @var array */ private array $params = []; - /** @var array */ - private array $options; - /** * @param array $options */ - public function __construct(array $options) + public function __construct(private readonly array $options) { - $this->options = $options; } public function onKernelRequest(RequestEvent $event): void diff --git a/src/Templating/PaginationHelper.php b/src/Templating/PaginationHelper.php index e7c25ba0..c7100797 100644 --- a/src/Templating/PaginationHelper.php +++ b/src/Templating/PaginationHelper.php @@ -16,14 +16,10 @@ */ final class PaginationHelper extends Helper { - protected PhpEngine $templating; - - protected Processor $processor; - - public function __construct(Processor $processor, PhpEngine $templating) - { - $this->processor = $processor; - $this->templating = $templating; + public function __construct( + private readonly Processor $processor, + private readonly PhpEngine $templating + ) { } /** diff --git a/src/Twig/Extension/PaginationExtension.php b/src/Twig/Extension/PaginationExtension.php index b88997c7..993ab3a3 100644 --- a/src/Twig/Extension/PaginationExtension.php +++ b/src/Twig/Extension/PaginationExtension.php @@ -3,6 +3,7 @@ namespace Knp\Bundle\PaginatorBundle\Twig\Extension; use Twig\Extension\AbstractExtension; +use Twig\TwigFilter; use Twig\TwigFunction; final class PaginationExtension extends AbstractExtension @@ -16,6 +17,7 @@ public function getFunctions(): array new TwigFunction('knp_pagination_rel_links', [PaginationRuntime::class, 'rel_links'], $options), new TwigFunction('knp_pagination_sortable', [PaginationRuntime::class, 'sortable'], $options), new TwigFunction('knp_pagination_filter', [PaginationRuntime::class, 'filter'], $options), + new TwigFunction('knp_pagination_query', [PaginationRuntime::class, 'getQueryParams']), ]; } } diff --git a/src/Twig/Extension/PaginationRuntime.php b/src/Twig/Extension/PaginationRuntime.php index b1b08fa7..8f30a36a 100644 --- a/src/Twig/Extension/PaginationRuntime.php +++ b/src/Twig/Extension/PaginationRuntime.php @@ -9,11 +9,11 @@ final class PaginationRuntime implements RuntimeExtensionInterface { - private Processor $processor; - - public function __construct(Processor $processor) - { - $this->processor = $processor; + public function __construct( + private readonly Processor $processor, + private readonly string $pageName = 'page', + private readonly bool $skipFirstPageLink = false, + ) { } /** @@ -107,4 +107,22 @@ public function filter( $this->processor->filter($pagination, $fields, $options ?? [], $params ?? []) ); } + + /** + * @param array $query + * @param int $page + * @return array + */ + public function getQueryParams(array $query, int $page): array + { + if ($page === 1 && $this->skipFirstPageLink) { + if (isset($query[$this->pageName])) { + unset($query[$this->pageName]); + } + + return $query; + } + + return array_merge($query, [$this->pageName => $page]); + } } diff --git a/templates/Pagination/bulma_pagination.html.twig b/templates/Pagination/bulma_pagination.html.twig index 318dd201..53ae1aae 100644 --- a/templates/Pagination/bulma_pagination.html.twig +++ b/templates/Pagination/bulma_pagination.html.twig @@ -13,13 +13,13 @@ {% if pageCount > 1 %}