3 namespace Drupal\views\Plugin\views\pager;
5 use Drupal\Core\Form\FormStateInterface;
6 use Drupal\views\Plugin\views\PluginBase;
9 * @defgroup views_pager_plugins Views pager plugins
11 * Plugins to handle paging in views.
13 * Pager plugins take care of everything regarding pagers, including figuring
14 * out the total number of items to render, setting up the query for paging,
15 * and setting up the pager.
17 * Pager plugins extend \Drupal\views\Plugin\views\pager\PagerPluginBase. They
18 * must be annotated with \Drupal\views\Annotation\ViewsPager annotation,
19 * and they must be in namespace directory Plugin\views\pager.
21 * @ingroup views_plugins
26 * Base class for views pager plugins.
28 abstract class PagerPluginBase extends PluginBase {
30 public $current_page = NULL;
32 public $total_items = 0;
37 protected $usesOptions = TRUE;
40 * Get how many items per page this pager will display.
42 * All but the leanest pagers should probably return a value here, so
43 * most pagers will not need to override this method.
45 public function getItemsPerPage() {
46 return isset($this->options['items_per_page']) ? $this->options['items_per_page'] : 0;
50 * Set how many items per page this pager will display.
52 * This is mostly used for things that will override the value.
54 public function setItemsPerPage($items) {
55 $this->options['items_per_page'] = $items;
59 * Get the page offset, or how many items to skip.
61 * Even pagers that don't actually page can skip items at the beginning,
62 * so few pagers will need to override this method.
64 public function getOffset() {
65 return isset($this->options['offset']) ? $this->options['offset'] : 0;
69 * Set the page offset, or how many items to skip.
71 public function setOffset($offset) {
72 $this->options['offset'] = $offset;
76 * Get the current page.
78 * If NULL, we do not know what the current page is.
80 public function getCurrentPage() {
81 return $this->current_page;
85 * Set the current page.
88 * If provided, the page number will be set to this. If NOT provided,
89 * the page number will be set from the global page array.
91 public function setCurrentPage($number = NULL) {
92 if (!is_numeric($number) || $number < 0) {
95 $this->current_page = $number;
99 * Get the total number of items.
101 * If NULL, we do not yet know what the total number of items are.
103 public function getTotalItems() {
104 return $this->total_items;
108 * Get the pager id, if it exists
110 public function getPagerId() {
111 return isset($this->options['id']) ? $this->options['id'] : 0;
115 * Provide the default form form for validating options
117 public function validateOptionsForm(&$form, FormStateInterface $form_state) {}
120 * Provide the default form form for submitting options
122 public function submitOptionsForm(&$form, FormStateInterface $form_state) {}
125 * Return a string to display as the clickable title for the
128 public function summaryTitle() {
129 return $this->t('Unknown');
133 * Determine if this pager actually uses a pager.
135 * Only a couple of very specific pagers will set this to false.
137 public function usePager() {
142 * Determine if a pager needs a count query.
144 * If a pager needs a count query, a simple query
146 public function useCountQuery() {
151 * Execute the count query, which will be done just prior to the query
152 * itself being executed.
154 public function executeCountQuery(&$count_query) {
155 $this->total_items = $count_query->execute()->fetchField();
156 if (!empty($this->options['offset'])) {
157 $this->total_items -= $this->options['offset'];
159 // Prevent from being negative.
160 $this->total_items = max(0, $this->total_items);
162 return $this->total_items;
166 * If there are pagers that need global values set, this method can
167 * be used to set them. It will be called after the query is run.
169 public function updatePageInfo() {
174 * Modify the query for paging
176 * This is called during the build phase and can directly modify the query.
178 public function query() {}
181 * Perform any needed actions just prior to the query executing.
183 public function preExecute(&$query) {}
186 * Perform any needed actions just after the query executing.
188 public function postExecute(&$result) {}
191 * Perform any needed actions just before rendering.
193 public function preRender(&$result) {}
196 * Return the renderable array of the pager.
198 * Called during the view render process.
201 * Any extra GET parameters that should be retained, such as exposed
204 public function render($input) {}
207 * Determine if there are more records available.
209 * This is primarily used to control the display of a more link.
211 public function hasMoreRecords() {
212 return $this->getItemsPerPage()
213 && $this->total_items > (intval($this->current_page) + 1) * $this->getItemsPerPage();
216 public function exposedFormAlter(&$form, FormStateInterface $form_state) {}
218 public function exposedFormValidate(&$form, FormStateInterface $form_state) {}
220 public function exposedFormSubmit(&$form, FormStateInterface $form_state, &$exclude) {}
222 public function usesExposed() {
226 protected function itemsPerPageExposed() {
230 protected function isOffsetExposed() {