Security update for Core, with self-updated composer
[yaffs-website] / web / core / modules / views / src / Plugin / views / pager / PagerPluginBase.php
1 <?php
2
3 namespace Drupal\views\Plugin\views\pager;
4
5 use Drupal\Core\Form\FormStateInterface;
6 use Drupal\views\Plugin\views\PluginBase;
7
8 /**
9  * @defgroup views_pager_plugins Views pager plugins
10  * @{
11  * Plugins to handle paging in views.
12  *
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.
16  *
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.
20  *
21  * @ingroup views_plugins
22  * @see plugin_api
23  */
24
25 /**
26  * Base class for views pager plugins.
27  */
28 abstract class PagerPluginBase extends PluginBase {
29
30   public $current_page = NULL;
31
32   public $total_items = 0;
33
34   /**
35    * {@inheritdoc}
36    */
37   protected $usesOptions = TRUE;
38
39   /**
40    * Get how many items per page this pager will display.
41    *
42    * All but the leanest pagers should probably return a value here, so
43    * most pagers will not need to override this method.
44    */
45   public function getItemsPerPage() {
46     return isset($this->options['items_per_page']) ? $this->options['items_per_page'] : 0;
47   }
48
49   /**
50    * Set how many items per page this pager will display.
51    *
52    * This is mostly used for things that will override the value.
53    */
54   public function setItemsPerPage($items) {
55     $this->options['items_per_page'] = $items;
56   }
57
58   /**
59    * Get the page offset, or how many items to skip.
60    *
61    * Even pagers that don't actually page can skip items at the beginning,
62    * so few pagers will need to override this method.
63    */
64   public function getOffset() {
65     return isset($this->options['offset']) ? $this->options['offset'] : 0;
66   }
67
68   /**
69    * Set the page offset, or how many items to skip.
70    */
71   public function setOffset($offset) {
72     $this->options['offset'] = $offset;
73   }
74
75   /**
76    * Get the current page.
77    *
78    * If NULL, we do not know what the current page is.
79    */
80   public function getCurrentPage() {
81     return $this->current_page;
82   }
83
84   /**
85    * Set the current page.
86    *
87    * @param $number
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.
90    */
91   public function setCurrentPage($number = NULL) {
92     if (!is_numeric($number) || $number < 0) {
93       $number = 0;
94     }
95     $this->current_page = $number;
96   }
97
98   /**
99    * Get the total number of items.
100    *
101    * If NULL, we do not yet know what the total number of items are.
102    */
103   public function getTotalItems() {
104     return $this->total_items;
105   }
106
107   /**
108    * Get the pager id, if it exists
109    */
110   public function getPagerId() {
111     return isset($this->options['id']) ? $this->options['id'] : 0;
112   }
113
114   /**
115    * Provide the default form form for validating options
116    */
117   public function validateOptionsForm(&$form, FormStateInterface $form_state) {}
118
119   /**
120    * Provide the default form form for submitting options
121    */
122   public function submitOptionsForm(&$form, FormStateInterface $form_state) {}
123
124   /**
125    * Return a string to display as the clickable title for the
126    * pager plugin.
127    */
128   public function summaryTitle() {
129     return $this->t('Unknown');
130   }
131
132   /**
133    * Determine if this pager actually uses a pager.
134    *
135    * Only a couple of very specific pagers will set this to false.
136    */
137   public function usePager() {
138     return TRUE;
139   }
140
141   /**
142    * Determine if a pager needs a count query.
143    *
144    * If a pager needs a count query, a simple query
145    */
146   public function useCountQuery() {
147     return TRUE;
148   }
149
150   /**
151    * Execute the count query, which will be done just prior to the query
152    * itself being executed.
153    */
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'];
158     }
159     // Prevent from being negative.
160     $this->total_items = max(0, $this->total_items);
161
162     return $this->total_items;
163   }
164
165   /**
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.
168    */
169   public function updatePageInfo() {
170
171   }
172
173   /**
174    * Modify the query for paging
175    *
176    * This is called during the build phase and can directly modify the query.
177    */
178   public function query() {}
179
180   /**
181    * Perform any needed actions just prior to the query executing.
182    */
183   public function preExecute(&$query) {}
184
185   /**
186    * Perform any needed actions just after the query executing.
187    */
188   public function postExecute(&$result) {}
189
190   /**
191    * Perform any needed actions just before rendering.
192    */
193   public function preRender(&$result) {}
194
195   /**
196    * Return the renderable array of the pager.
197    *
198    * Called during the view render process.
199    *
200    * @param $input
201    *   Any extra GET parameters that should be retained, such as exposed
202    *   input.
203    */
204   public function render($input) {}
205
206   /**
207    * Determine if there are more records available.
208    *
209    * This is primarily used to control the display of a more link.
210    */
211   public function hasMoreRecords() {
212     return $this->getItemsPerPage()
213       && $this->total_items > (intval($this->current_page) + 1) * $this->getItemsPerPage();
214   }
215
216   public function exposedFormAlter(&$form, FormStateInterface $form_state) {}
217
218   public function exposedFormValidate(&$form, FormStateInterface $form_state) {}
219
220   public function exposedFormSubmit(&$form, FormStateInterface $form_state, &$exclude) {}
221
222   public function usesExposed() {
223     return FALSE;
224   }
225
226   protected function itemsPerPageExposed() {
227     return FALSE;
228   }
229
230   protected function isOffsetExposed() {
231     return FALSE;
232   }
233
234 }
235
236 /**
237  * @}
238  */