3 namespace Drupal\views\Plugin\views\pager;
5 use Drupal\Core\Cache\Cache;
6 use Drupal\Core\Cache\CacheableDependencyInterface;
7 use Drupal\Core\Form\FormStateInterface;
10 * A common base class for sql based pager.
12 abstract class SqlBase extends PagerPluginBase implements CacheableDependencyInterface {
14 protected function defineOptions() {
15 $options = parent::defineOptions();
16 $options['items_per_page'] = ['default' => 10];
17 $options['offset'] = ['default' => 0];
18 $options['id'] = ['default' => 0];
19 $options['total_pages'] = ['default' => ''];
20 $options['expose'] = [
22 'items_per_page' => ['default' => FALSE],
23 'items_per_page_label' => ['default' => $this->t('Items per page')],
24 'items_per_page_options' => ['default' => '5, 10, 25, 50'],
25 'items_per_page_options_all' => ['default' => FALSE],
26 'items_per_page_options_all_label' => ['default' => $this->t('- All -')],
28 'offset' => ['default' => FALSE],
29 'offset_label' => ['default' => $this->t('Offset')],
34 'previous' => ['default' => $this->t('‹ Previous')],
35 'next' => ['default' => $this->t('Next ›')],
42 * Provide the default form for setting options.
44 public function buildOptionsForm(&$form, FormStateInterface $form_state) {
45 parent::buildOptionsForm($form, $form_state);
46 $pager_text = $this->displayHandler->getPagerText();
47 $form['items_per_page'] = [
48 '#title' => $pager_text['items per page title'],
50 '#description' => $pager_text['items per page description'],
51 '#default_value' => $this->options['items_per_page'],
56 '#title' => $this->t('Offset (number of items to skip)'),
57 '#description' => $this->t('For example, set this to 3 and the first 3 items will not be displayed.'),
58 '#default_value' => $this->options['offset'],
63 '#title' => $this->t('Pager ID'),
64 '#description' => $this->t("Unless you're experiencing problems with pagers related to this view, you should leave this at 0. If using multiple pagers on one page you may need to set this number to a higher value so as not to conflict within the ?page= array. Large values will add a lot of commas to your URLs, so avoid if possible."),
65 '#default_value' => $this->options['id'],
68 $form['total_pages'] = [
70 '#title' => $this->t('Number of pages'),
71 '#description' => $this->t('Leave empty to show all pages.'),
72 '#default_value' => $this->options['total_pages'],
79 '#title' => $this->t('Pager link labels'),
83 $form['tags']['previous'] = [
84 '#type' => 'textfield',
85 '#title' => $this->t('Previous page link text'),
86 '#default_value' => $this->options['tags']['previous'],
89 $form['tags']['next'] = [
90 '#type' => 'textfield',
91 '#title' => $this->t('Next page link text'),
92 '#default_value' => $this->options['tags']['next'],
99 '#title' => $this->t('Exposed options'),
101 '#description' => $this->t('Allow user to control selected display options for this view.'),
104 $form['expose']['items_per_page'] = [
105 '#type' => 'checkbox',
106 '#title' => $this->t('Allow user to control the number of items displayed in this view'),
107 '#default_value' => $this->options['expose']['items_per_page'],
110 $form['expose']['items_per_page_label'] = [
111 '#type' => 'textfield',
112 '#title' => $this->t('Items per page label'),
114 '#default_value' => $this->options['expose']['items_per_page_label'],
117 'input[name="pager_options[expose][items_per_page]"]' => ['checked' => FALSE],
122 $form['expose']['items_per_page_options'] = [
123 '#type' => 'textfield',
124 '#title' => $this->t('Exposed items per page options'),
126 '#description' => $this->t('Set between which values the user can choose when determining the items per page. Separated by comma.'),
127 '#default_value' => $this->options['expose']['items_per_page_options'],
130 'input[name="pager_options[expose][items_per_page]"]' => ['checked' => FALSE],
135 $form['expose']['items_per_page_options_all'] = [
136 '#type' => 'checkbox',
137 '#title' => $this->t('Allow user to display all items'),
138 '#default_value' => $this->options['expose']['items_per_page_options_all'],
141 $form['expose']['items_per_page_options_all_label'] = [
142 '#type' => 'textfield',
143 '#title' => $this->t('All items label'),
144 '#default_value' => $this->options['expose']['items_per_page_options_all_label'],
147 'input[name="pager_options[expose][items_per_page_options_all]"]' => ['checked' => FALSE],
152 $form['expose']['offset'] = [
153 '#type' => 'checkbox',
154 '#title' => $this->t('Allow user to specify number of items skipped from beginning of this view.'),
155 '#default_value' => $this->options['expose']['offset'],
158 $form['expose']['offset_label'] = [
159 '#type' => 'textfield',
160 '#title' => $this->t('Offset label'),
162 '#default_value' => $this->options['expose']['offset_label'],
165 'input[name="pager_options[expose][offset]"]' => ['checked' => FALSE],
171 public function validateOptionsForm(&$form, FormStateInterface $form_state) {
172 // Only accept integer values.
174 $exposed_options = $form_state->getValue(['pager_options', 'expose', 'items_per_page_options']);
175 if (strpos($exposed_options, '.') !== FALSE) {
178 $options = explode(',', $exposed_options);
179 if (!$error && is_array($options)) {
180 foreach ($options as $option) {
181 if (!is_numeric($option) || intval($option) == 0) {
190 $form_state->setErrorByName('pager_options][expose][items_per_page_options', $this->t('Insert a list of integer numeric values separated by commas: e.g: 10, 20, 50, 100'));
193 // Make sure that the items_per_page is part of the expose settings.
194 if (!$form_state->isValueEmpty(['pager_options', 'expose', 'items_per_page']) && !$form_state->isValueEmpty(['pager_options', 'items_per_page'])) {
195 $items_per_page = $form_state->getValue(['pager_options', 'items_per_page']);
196 if (array_search($items_per_page, $options) === FALSE) {
197 $form_state->setErrorByName('pager_options][expose][items_per_page_options', $this->t("The <em>Exposed items per page</em> field's options must include the value from the <em>Items per page</em> field (@items_per_page).",
198 ['@items_per_page' => $items_per_page])
204 public function query() {
205 if ($this->itemsPerPageExposed()) {
206 $query = $this->view->getRequest()->query;
207 $items_per_page = $query->get('items_per_page');
208 if ($items_per_page > 0) {
209 $this->options['items_per_page'] = $items_per_page;
211 elseif ($items_per_page == 'All' && $this->options['expose']['items_per_page_options_all']) {
212 $this->options['items_per_page'] = 0;
215 if ($this->isOffsetExposed()) {
216 $query = $this->view->getRequest()->query;
217 $offset = $query->get('offset');
218 if (isset($offset) && $offset >= 0) {
219 $this->options['offset'] = $offset;
223 $limit = $this->options['items_per_page'];
224 $offset = $this->current_page * $this->options['items_per_page'] + $this->options['offset'];
225 if (!empty($this->options['total_pages'])) {
226 if ($this->current_page >= $this->options['total_pages']) {
227 $limit = $this->options['items_per_page'];
228 $offset = $this->options['total_pages'] * $this->options['items_per_page'];
232 $this->view->query->setLimit($limit);
233 $this->view->query->setOffset($offset);
237 * Set the current page.
240 * If provided, the page number will be set to this. If NOT provided,
241 * the page number will be set from the global page array.
243 public function setCurrentPage($number = NULL) {
244 if (isset($number)) {
245 $this->current_page = max(0, $number);
249 // If the current page number was not specified, extract it from the global
251 global $pager_page_array;
253 if (empty($pager_page_array)) {
254 $pager_page_array = [];
257 // Fill in missing values in the global page array, in case the global page
258 // array hasn't been initialized before.
259 $page = $this->view->getRequest()->query->get('page');
260 $page = isset($page) ? explode(',', $page) : [];
262 for ($i = 0; $i <= $this->options['id'] || $i < count($pager_page_array); $i++) {
263 $pager_page_array[$i] = empty($page[$i]) ? 0 : $page[$i];
266 // Don't allow the number to be less than zero.
267 $this->current_page = max(0, intval($pager_page_array[$this->options['id']]));
270 public function getPagerTotal() {
271 if ($items_per_page = intval($this->getItemsPerPage())) {
272 return ceil($this->total_items / $items_per_page);
280 * Update global paging info.
282 * This is called after the count query has been run to set the total
283 * items available and to update the current page if the requested
284 * page is out of range.
286 public function updatePageInfo() {
287 if (!empty($this->options['total_pages'])) {
288 if (($this->options['total_pages'] * $this->options['items_per_page']) < $this->total_items) {
289 $this->total_items = $this->options['total_pages'] * $this->options['items_per_page'];
293 // Don't set pager settings for items per page = 0.
294 $items_per_page = $this->getItemsPerPage();
295 if (!empty($items_per_page)) {
296 // Dump information about what we already know into the globals.
297 global $pager_page_array, $pager_total, $pager_total_items, $pager_limits;
299 $pager_limits[$this->options['id']] = $this->options['items_per_page'];
300 // Set the item count for the pager.
301 $pager_total_items[$this->options['id']] = $this->total_items;
302 // Calculate and set the count of available pages.
303 $pager_total[$this->options['id']] = $this->getPagerTotal();
305 // See if the requested page was within range:
306 if ($this->current_page >= $pager_total[$this->options['id']]) {
307 // Pages are numbered from 0 so if there are 10 pages, the last page is 9.
308 $this->setCurrentPage($pager_total[$this->options['id']] - 1);
311 // Put this number in to guarantee that we do not generate notices when the pager
312 // goes to look for it later.
313 $pager_page_array[$this->options['id']] = $this->current_page;
317 public function usesExposed() {
318 return $this->itemsPerPageExposed() || $this->isOffsetExposed();
321 protected function itemsPerPageExposed() {
322 return !empty($this->options['expose']['items_per_page']);
325 protected function isOffsetExposed() {
326 return !empty($this->options['expose']['offset']);
329 public function exposedFormAlter(&$form, FormStateInterface $form_state) {
330 if ($this->itemsPerPageExposed()) {
331 $options = explode(',', $this->options['expose']['items_per_page_options']);
332 $sanitized_options = [];
333 if (is_array($options)) {
334 foreach ($options as $option) {
335 $sanitized_options[intval($option)] = intval($option);
337 if (!empty($this->options['expose']['items_per_page_options_all']) && !empty($this->options['expose']['items_per_page_options_all_label'])) {
338 $sanitized_options['All'] = $this->options['expose']['items_per_page_options_all_label'];
340 $form['items_per_page'] = [
342 '#title' => $this->options['expose']['items_per_page_label'],
343 '#options' => $sanitized_options,
344 '#default_value' => $this->getItemsPerPage(),
349 if ($this->isOffsetExposed()) {
351 '#type' => 'textfield',
354 '#title' => $this->options['expose']['offset_label'],
355 '#default_value' => $this->getOffset(),
360 public function exposedFormValidate(&$form, FormStateInterface $form_state) {
361 if (!$form_state->isValueEmpty('offset') && trim($form_state->getValue('offset'))) {
362 if (!is_numeric($form_state->getValue('offset')) || $form_state->getValue('offset') < 0) {
363 $form_state->setErrorByName('offset', $this->t('Offset must be a number greater than or equal to 0.'));
371 public function getCacheMaxAge() {
372 return Cache::PERMANENT;
378 public function getCacheContexts() {
379 // The rendered link needs to play well with any other query parameter used
380 // on the page, like other pagers and exposed filter.
381 return ['url.query_args'];
387 public function getCacheTags() {