Skip to content
This repository has been archived by the owner on Nov 25, 2020. It is now read-only.

Commit

Permalink
Merge pull request #140 from bobvandevijver/patch-1
Browse files Browse the repository at this point in the history
Make first page param configurable
  • Loading branch information
pablodip committed Mar 16, 2016
2 parents 1c5abdd + b3226be commit 27ccf0f
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ public function viewWithOptionsAction()
return $this->renderPagerfanta('viewWithOptions');
}

public function viewWithFirstPageParamAction(Request $request)
{
return $this->defaultWithRequestAction($request, 'viewWithFirstPageParam');
}

public function defaultTranslatedViewAction()
{
return $this->renderPagerfanta('defaultTranslatedView');
Expand All @@ -54,9 +59,9 @@ public function viewWithRouteParamsAction($test = null)
return $this->renderPagerfanta('viewWithRouteParams');
}

public function defaultWithRequestAction(Request $request)
public function defaultWithRequestAction(Request $request, $name = 'defaultView')
{
$template = $this->buildTemplateName('defaultView');
$template = $this->buildTemplateName($name);
$pagerfanta = $this->createPagerfanta();
$pagerfanta->setMaxPerPage($request->query->get('maxPerPage', 10));
$pagerfanta->setCurrentPage($request->query->get('currentPage', 1));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,7 @@ pagerfanta_my_view_1:
pagerfanta_correct_view:
path: /pagerfanta/custom-page
defaults: { _controller: WhiteOctoberPagerfantaTestBundle:Pagerfanta:defaultWithRequest }

pagerfanta_view_with_first_page_param:
path: /pagerfanta/view-with-first-page-param
defaults: { _controller: WhiteOctoberPagerfantaTestBundle:Pagerfanta:viewWithFirstPageParam }
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{{ pagerfanta(pagerfanta, 'default', {'omitFirstPage': false}) }}
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,27 @@ public function testFirstPageLinkDoesNotHaveParam()
);
}

/**
* @test
*/
public function testFirstPageParam()
{
$this->assertView('view-with-first-page-param?currentPage=2', <<<EOF
<nav>
<a href="/pagerfanta/view-with-first-page-param?currentPage=2&page=1">Previous</a>
<a href="/pagerfanta/view-with-first-page-param?currentPage=2&page=1">1</a>
<span class="current">2</span>
<a href="/pagerfanta/view-with-first-page-param?currentPage=2&page=3">3</a>
<a href="/pagerfanta/view-with-first-page-param?currentPage=2&page=4">4</a>
<a href="/pagerfanta/view-with-first-page-param?currentPage=2&page=5">5</a>
<span class="dots">...</span>
<a href="/pagerfanta/view-with-first-page-param?currentPage=2&page=10">10</a>
<a href="/pagerfanta/view-with-first-page-param?currentPage=2&page=3">Next</a>
</nav>
EOF
);
}

/**
* @test
*/
Expand Down
13 changes: 8 additions & 5 deletions Twig/PagerfantaExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ private function createRouteGenerator($options = array())
'routeName' => null,
'routeParams' => array(),
'pageParameter' => '[page]',
'omitFirstPage' => true
), $options);

$router = $this->container->get('router');
Expand Down Expand Up @@ -133,13 +134,15 @@ private function createRouteGenerator($options = array())
$routeName = $options['routeName'];
$routeParams = $options['routeParams'];
$pagePropertyPath = new PropertyPath($options['pageParameter']);
$omitFirstPage = $options['omitFirstPage'];

return function($page) use($router, $routeName, $routeParams, $pagePropertyPath) {
return function($page) use($router, $routeName, $routeParams, $pagePropertyPath, $omitFirstPage) {
$propertyAccessor = PropertyAccess::createPropertyAccessor();
$propertyAccessor->setValue($routeParams, $pagePropertyPath, $page);

$pageParam = $page > 1 ? $page : null;
$propertyAccessor->setValue($routeParams, $pagePropertyPath, $pageParam);
if($omitFirstPage){
$propertyAccessor->setValue($routeParams, $pagePropertyPath, $page > 1 ? $page : null);
} else {
$propertyAccessor->setValue($routeParams, $pagePropertyPath, $page);
}

return $router->generate($routeName, $routeParams);
};
Expand Down

0 comments on commit 27ccf0f

Please sign in to comment.