1<?php
2
3namespace Drupal\views\Plugin\views\pager;
4
5/**
6 * The plugin to handle mini pager.
7 *
8 * @ingroup views_pager_plugins
9 *
10 * @ViewsPager(
11 *   id = "mini",
12 *   title = @Translation("Paged output, mini pager"),
13 *   short_title = @Translation("Mini"),
14 *   help = @Translation("A simple pager containing previous and next links."),
15 *   theme = "views_mini_pager"
16 * )
17 */
18class Mini extends SqlBase {
19
20  /**
21   * Overrides \Drupal\views\Plugin\views\pager\PagerPlugin::defineOptions().
22   *
23   * Provides sane defaults for the next/previous links.
24   */
25  public function defineOptions() {
26    $options = parent::defineOptions();
27
28    $options['tags']['contains']['previous']['default'] = '‹‹';
29    $options['tags']['contains']['next']['default'] = '››';
30
31    return $options;
32  }
33
34  /**
35   * {@inheritdoc}
36   */
37  public function summaryTitle() {
38    if (!empty($this->options['offset'])) {
39      return $this->formatPlural($this->options['items_per_page'], 'Mini pager, @count item, skip @skip', 'Mini pager, @count items, skip @skip', ['@count' => $this->options['items_per_page'], '@skip' => $this->options['offset']]);
40    }
41    return $this->formatPlural($this->options['items_per_page'], 'Mini pager, @count item', 'Mini pager, @count items', ['@count' => $this->options['items_per_page']]);
42  }
43
44  /**
45   * {@inheritdoc}
46   */
47  public function query() {
48    parent::query();
49
50    // Only modify the query if we don't want to do a total row count
51    if (!$this->view->get_total_rows) {
52      // Don't query for the next page if we have a pager that has a limited
53      // amount of pages.
54      if ($this->getItemsPerPage() > 0 && (empty($this->options['total_pages']) || ($this->getCurrentPage() < $this->options['total_pages']))) {
55        // Increase the items in the query in order to be able to find out
56        // whether there is another page.
57        $limit = $this->view->query->getLimit();
58        $limit += 1;
59        $this->view->query->setLimit($limit);
60      }
61    }
62  }
63
64  /**
65   * {@inheritdoc}
66   */
67  public function useCountQuery() {
68    return FALSE;
69  }
70
71  /**
72   * {@inheritdoc}
73   */
74  public function postExecute(&$result) {
75    // Only modify the result if we didn't do a total row count
76    if (!$this->view->get_total_rows) {
77      $this->total_items = $this->getCurrentPage() * $this->getItemsPerPage() + count($result);
78      // query() checks if we need a next link by setting limit 1 record past
79      // this page If we got the extra record we need to remove it before we
80      // render the result.
81      if ($this->getItemsPerPage() > 0 && count($result) > $this->getItemsPerPage()) {
82        array_pop($result);
83      }
84    }
85  }
86
87  /**
88   * {@inheritdoc}
89   */
90  public function render($input) {
91    // The 1, 3 indexes are correct, see template_preprocess_pager().
92    $tags = [
93      1 => $this->options['tags']['previous'],
94      3 => $this->options['tags']['next'],
95    ];
96    return [
97      '#theme' => $this->themeFunctions(),
98      '#tags' => $tags,
99      '#element' => $this->options['id'],
100      '#parameters' => $input,
101      '#route_name' => !empty($this->view->live_preview) ? '<current>' : '<none>',
102    ];
103  }
104
105}
106