Skip to content

Commit 1fffbf3

Browse files
committed
support of query parameter name changes, closes KnpLabs#87
1 parent 3168032 commit 1fffbf3

File tree

5 files changed

+46
-26
lines changed

5 files changed

+46
-26
lines changed

DependencyInjection/Configuration.php

+9
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,15 @@ public function getConfigTreeBuilder()
1414
$builder->root('knp_paginator')
1515
->addDefaultsIfNotSet()
1616
->children()
17+
->arrayNode('default_options')
18+
->addDefaultsIfNotSet()
19+
->children()
20+
->scalarNode('sort_field_name')->defaultValue('sort')->end()
21+
->scalarNode('sort_direction_name')->defaultValue('direction')->end()
22+
->scalarNode('page_name')->defaultValue('page')->end()
23+
->booleanNode('distinct')->defaultTrue()->end()
24+
->end()
25+
->end()
1726
->arrayNode('template')
1827
->addDefaultsIfNotSet()
1928
->children()

DependencyInjection/KnpPaginatorExtension.php

+8
Original file line numberDiff line numberDiff line change
@@ -29,5 +29,13 @@ public function load(array $configs, ContainerBuilder $container)
2929
$container->setParameter('knp_paginator.template.pagination', $config['template']['pagination']);
3030
$container->setParameter('knp_paginator.template.sortable', $config['template']['sortable']);
3131
$container->setParameter('knp_paginator.page_range', $config['page_range']);
32+
33+
$paginatorDef = $container->getDefinition('knp_paginator');
34+
$paginatorDef->addMethodCall('setDefaultPaginatorOptions', array(array(
35+
'pageParameterName' => $config['default_options']['page_name'],
36+
'sortFieldParameterName' => $config['default_options']['sort_field_name'],
37+
'sortDirectionParameterName' => $config['default_options']['sort_direction_name'],
38+
'distinct' => $config['default_options']['distinct']
39+
)));
3240
}
3341
}

Pagination/SlidingPagination.php

+23-19
Original file line numberDiff line numberDiff line change
@@ -47,16 +47,6 @@ public function setTemplate($template)
4747
$this->template = $template;
4848
}
4949

50-
public function setExtraViewParams(array $params)
51-
{
52-
$this->extraViewParams = $params;
53-
}
54-
55-
public function setExtraViewParam($name, $param)
56-
{
57-
$this->extraViewParams[$name] = $param;
58-
}
59-
6050
public function setPageRange($range)
6151
{
6252
$this->pageRange = abs(intval($range));
@@ -77,9 +67,13 @@ public function render($template = null, array $queryParams = array(), array $vi
7767
}
7868
$data = $this->getPaginationData();
7969
$data['route'] = $this->route;
80-
$data['alias'] = $this->alias;
8170
$data['query'] = array_merge($this->params, $queryParams);
82-
$data = array_merge($this->extraViewParams, $viewParams, $data/* cannot be broken*/);
71+
$data = array_merge(
72+
$this->paginatorOptions, // options given to paginator when paginated
73+
$this->customParameters, // all custom parameters for view
74+
$viewParams, // additional custom parameters for view
75+
$data // merging base route parameters last, to avoid broke of integrity
76+
);
8377
return $this->engine->render($this->template, $data);
8478
}
8579

@@ -105,11 +99,15 @@ public function sortable($title, $key, $options = array(), $params = array(), $t
10599
), $options);
106100

107101
$params = array_merge($this->params, $params);
108-
$direction = isset($options[$this->alias.'direction']) ? $options[$this->alias.'direction'] : 'asc';
102+
$direction = isset($options[$this->getPaginatorOption('sortDirectionParameterName')]) ?
103+
$options[$this->getPaginatorOption('sortDirectionParameterName')] : 'asc'
104+
;
109105

110-
$sorted = isset($params[$this->alias.'sort']) && $params[$this->alias.'sort'] == $key;
106+
$sorted = isset($params[$this->getPaginatorOption('sortFieldParameterName')])
107+
&& $params[$this->getPaginatorOption('sortFieldParameterName')] == $key
108+
;
111109
if ($sorted) {
112-
$direction = $params[$this->alias.'direction'];
110+
$direction = $params[$this->getPaginatorOption('sortDirectionParameterName')];
113111
$direction = (strtolower($direction) == 'asc') ? 'desc' : 'asc';
114112
$class = $direction == 'asc' ? 'desc' : 'asc';
115113
if (isset($options['class'])) {
@@ -125,7 +123,11 @@ public function sortable($title, $key, $options = array(), $params = array(), $t
125123
}
126124
$params = array_merge(
127125
$params,
128-
array($this->alias.'sort' => $key, $this->alias.'direction' => $direction, $this->alias.'page' => 1)
126+
array(
127+
$this->getPaginatorOption('sortFieldParameterName') => $key,
128+
$this->getPaginatorOption('sortDirectionParameterName') => $direction,
129+
$this->getPaginatorOption('pageParameterName') => 1 // reset to 1 on sort
130+
)
129131
);
130132

131133
$options['href'] = $this->routerHelper->generate($this->route, $params, $options['absolute']);
@@ -140,9 +142,11 @@ public function sortable($title, $key, $options = array(), $params = array(), $t
140142
$this->sortableTemplate = $template;
141143
}
142144

143-
$alias = $this->alias;
144-
145-
return $this->engine->render($this->sortableTemplate, compact('options', 'title', 'direction', 'sorted', 'key', 'alias'));
145+
return $this->engine->render($this->sortableTemplate, array_merge(
146+
$this->paginatorOptions,
147+
$this->customParameters,
148+
compact('options', 'title', 'direction', 'sorted', 'key')
149+
));
146150
}
147151

148152
public function getPaginationData()

Resources/views/Pagination/sliding.html.twig

+6-6
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,22 @@
22

33
{% if pageCount > 1 %}
44
<div class="pagination">
5-
{% if first is defined and current != first %}
5+
{% if first is defined and current != first %}
66
<span class="first">
7-
<a href="{{ path(route, query|merge({'page': first})) }}">&lt;&lt;</a>
7+
<a href="{{ path(route, query|merge({(pageParameterName): first})) }}">&lt;&lt;</a>
88
</span>
99
{% endif %}
1010

1111
{% if previous is defined %}
1212
<span class="previous">
13-
<a href="{{ path(route, query|merge({'page': previous})) }}">&lt;</a>
13+
<a href="{{ path(route, query|merge({(pageParameterName): previous})) }}">&lt;</a>
1414
</span>
1515
{% endif %}
1616

1717
{% for page in pagesInRange %}
1818
{% if page != current %}
1919
<span class="page">
20-
<a href="{{ path(route, query|merge({'page': page})) }}">{{ page }}</a>
20+
<a href="{{ path(route, query|merge({(pageParameterName): page})) }}">{{ page }}</a>
2121
</span>
2222
{% else %}
2323
<span class="current">{{ page }}</span>
@@ -27,13 +27,13 @@
2727

2828
{% if next is defined %}
2929
<span class="next">
30-
<a href="{{ path(route, query|merge({'page': next})) }}">&gt;</a>
30+
<a href="{{ path(route, query|merge({(pageParameterName): next})) }}">&gt;</a>
3131
</span>
3232
{% endif %}
3333

3434
{% if last is defined and current != last %}
3535
<span class="last">
36-
<a href="{{ path(route, query|merge({'page': last})) }}">&gt;&gt;</a>
36+
<a href="{{ path(route, query|merge({(pageParameterName): last})) }}">&gt;&gt;</a>
3737
</span>
3838
{% endif %}
3939
</div>

Subscriber/SlidingPaginationSubscriber.php

-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@ public function pagination(PaginationEvent $event)
5252
$this->params
5353
);
5454
$pagination->setUsedRoute($this->route);
55-
$pagination->setAlias($event->options['alias']);
5655
$pagination->setTemplate($this->options['defaultPaginationTemplate']);
5756
$pagination->setSortableTemplate($this->options['defaultSortableTemplate']);
5857
$pagination->setPageRange($this->options['defaultPageRange']);

0 commit comments

Comments
 (0)