Version 1
[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
160     return $this->total_items;
161   }
162
163   /**
164    * If there are pagers that need global values set, this method can
165    * be used to set them. It will be called after the query is run.
166    */
167   public function updatePageInfo() {
168
169   }
170
171   /**
172    * Modify the query for paging
173    *
174    * This is called during the build phase and can directly modify the query.
175    */
176   public function query() { }
177
178   /**
179    * Perform any needed actions just prior to the query executing.
180    */
181   public function preExecute(&$query) { }
182
183   /**
184    * Perform any needed actions just after the query executing.
185    */
186   public function postExecute(&$result) { }
187
188   /**
189    * Perform any needed actions just before rendering.
190    */
191   public function preRender(&$result) { }
192
193   /**
194    * Return the renderable array of the pager.
195    *
196    * Called during the view render process.
197    *
198    * @param $input
199    *   Any extra GET parameters that should be retained, such as exposed
200    *   input.
201    */
202   public function render($input) { }
203
204   /**
205    * Determine if there are more records available.
206    *
207    * This is primarily used to control the display of a more link.
208    */
209   public function hasMoreRecords() {
210     return $this->getItemsPerPage()
211       && $this->total_items > (intval($this->current_page) + 1) * $this->getItemsPerPage();
212   }
213
214   public function exposedFormAlter(&$form, FormStateInterface $form_state) { }
215
216   public function exposedFormValidate(&$form, FormStateInterface $form_state) { }
217
218   public function exposedFormSubmit(&$form, FormStateInterface $form_state, &$exclude) { }
219
220   public function usesExposed() {
221     return FALSE;
222   }
223
224   protected function itemsPerPageExposed() {
225     return FALSE;
226   }
227
228   protected function isOffsetExposed() {
229     return FALSE;
230   }
231
232 }
233
234 /**
235  * @}
236  */