-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathviews_pager_summary.module
136 lines (121 loc) · 4.33 KB
/
views_pager_summary.module
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
<?php
/**
* Implements hook_theme().
*
* Register views theming functions and those that are defined via views plugin
* definitions.
*/
function views_pager_summary_theme($existing, $type, $theme, $path) {
$hooks['views_pager_with_summary'] = array(
'variables' => array(
'tags' => array(),
'quantity' => 9,
'element' => 0,
'parameters' => array(),
'summary' => NULL,
),
);
return $hooks;
}
function template_preprocess_views_pager_with_summary(&$variables) {
$element = $variables['element'];
$parameters = $variables['parameters'];
$quantity = $variables['quantity'];
$route_name = isset($variables['route_name']) ? $variables['route_name'] : '<current>';
$route_parameters = isset($variables['route_parameters']) ? $variables['route_parameters'] : [];
global $pager_page_array, $pager_total;
// Nothing to do if there is only one page.
if ($pager_total[$element] <= 1) {
return;
}
$tags = $variables['tags'];
// Calculate various markers within this pager piece:
// Middle is used to "center" pages around the current page.
$pager_middle = ceil($quantity / 2);
// current is the page we are currently paged to.
$pager_current = $pager_page_array[$element] + 1;
// first is the first page listed by this pager piece (re quantity).
$pager_first = $pager_current - $pager_middle + 1;
// last is the last page listed by this pager piece (re quantity).
$pager_last = $pager_current + $quantity - $pager_middle;
// max is the maximum page number.
$pager_max = $pager_total[$element];
// End of marker calculations.
// Prepare for generation loop.
$i = $pager_first;
if ($pager_last > $pager_max) {
// Adjust "center" if at end of query.
$i = $i + ($pager_max - $pager_last);
$pager_last = $pager_max;
}
if ($i <= 0) {
// Adjust "center" if at start of query.
$pager_last = $pager_last + (1 - $i);
$i = 1;
}
// End of generation loop preparation.
$items = [];
// Create the "first" and "previous" links if we are not on the first page.
if ($pager_page_array[$element] > 0) {
$items['first'] = array();
$options = array(
'query' => pager_query_add_page($parameters, $element, 0),
);
$items['first']['href'] = \Drupal::url($route_name, $route_parameters, $options);
if (isset($tags[0])) {
$items['first']['text'] = $tags[0];
}
$items['previous'] = array();
$options = array(
'query' => pager_query_add_page($parameters, $element, $pager_page_array[$element] - 1),
);
$items['previous']['href'] = \Drupal::url($route_name, $route_parameters, $options);
if (isset($tags[1])) {
$items['previous']['text'] = $tags[1];
}
}
if ($i != $pager_max) {
// Add an ellipsis if there are further previous pages.
if ($i > 1) {
$variables['ellipses']['previous'] = TRUE;
}
// Now generate the actual pager piece.
for (; $i <= $pager_last && $i <= $pager_max; $i++) {
$options = array(
'query' => pager_query_add_page($parameters, $element, $i - 1),
);
$items['pages'][$i]['href'] = \Drupal::url($route_name, $route_parameters, $options);
if ($i == $pager_current) {
$variables['current'] = $i;
}
}
// Add an ellipsis if there are further next pages.
if ($i < $pager_max + 1) {
$variables['ellipses']['next'] = TRUE;
}
}
// Create the "next" and "last" links if we are not on the last page.
if ($pager_page_array[$element] < ($pager_max - 1)) {
$items['next'] = array();
$options = array(
'query' => pager_query_add_page($parameters, $element, $pager_page_array[$element] + 1),
);
$items['next']['href'] = \Drupal::url($route_name, $route_parameters, $options);
if (isset($tags[3])) {
$items['next']['text'] = $tags[3];
}
$items['last'] = array();
$options = array(
'query' => pager_query_add_page($parameters, $element, $pager_max - 1),
);
$items['last']['href'] = \Drupal::url($route_name, $route_parameters, $options);
if (isset($tags[4])) {
$items['last']['text'] = $tags[4];
}
}
$variables['items'] = $items;
// The rendered link needs to play well with any other query parameter used
// on the page, like exposed filters, so for the cacheability all query
// parameters matter.
$variables['#cache']['contexts'][] = 'url.query_args';
}