PageRenderTime 24ms CodeModel.GetById 31ms RepoModel.GetById 0ms app.codeStats 0ms

/web/core/modules/views/src/Plugin/views/pager/SqlBase.php

https://gitlab.com/andecode/theme-spark
PHP | 415 lines | 332 code | 36 blank | 47 comment | 24 complexity | 2b799a610662eb7bc2eb67cec61713a0 MD5 | raw file
  1. <?php
  2. namespace Drupal\views\Plugin\views\pager;
  3. use Drupal\Core\Cache\Cache;
  4. use Drupal\Core\Cache\CacheableDependencyInterface;
  5. use Drupal\Core\Form\FormStateInterface;
  6. use Drupal\Core\Pager\PagerManagerInterface;
  7. use Drupal\Core\Pager\PagerParametersInterface;
  8. use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
  9. use Symfony\Component\DependencyInjection\ContainerInterface;
  10. /**
  11. * A common base class for sql based pager.
  12. */
  13. abstract class SqlBase extends PagerPluginBase implements CacheableDependencyInterface, ContainerFactoryPluginInterface {
  14. /**
  15. * The pager manager.
  16. *
  17. * @var \Drupal\Core\Pager\PagerManagerInterface
  18. */
  19. protected $pagerManager;
  20. /**
  21. * The pager parameters.
  22. *
  23. * @var \Drupal\Core\Pager\PagerParametersInterface
  24. */
  25. protected $pagerParameters;
  26. /**
  27. * Constructs a SqlBase object.
  28. *
  29. * @param array $configuration
  30. * A configuration array containing information about the plugin instance.
  31. * @param string $plugin_id
  32. * The plugin_id for the plugin instance.
  33. * @param mixed $plugin_definition
  34. * The plugin implementation definition.
  35. * @param \Drupal\Core\Pager\PagerManagerInterface $pager_manager
  36. * The pager manager.
  37. * @param \Drupal\Core\Pager\PagerParametersInterface $pager_parameters
  38. * The pager parameters.
  39. */
  40. public function __construct(array $configuration, $plugin_id, $plugin_definition, PagerManagerInterface $pager_manager, PagerParametersInterface $pager_parameters) {
  41. parent::__construct($configuration, $plugin_id, $plugin_definition);
  42. $this->pagerManager = $pager_manager;
  43. $this->pagerParameters = $pager_parameters;
  44. }
  45. /**
  46. * {@inheritdoc}
  47. */
  48. public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
  49. return new static(
  50. $configuration,
  51. $plugin_id,
  52. $plugin_definition,
  53. $container->get('pager.manager'),
  54. $container->get('pager.parameters')
  55. );
  56. }
  57. protected function defineOptions() {
  58. $options = parent::defineOptions();
  59. $options['items_per_page'] = ['default' => 10];
  60. $options['offset'] = ['default' => 0];
  61. $options['id'] = ['default' => 0];
  62. $options['total_pages'] = ['default' => ''];
  63. $options['expose'] = [
  64. 'contains' => [
  65. 'items_per_page' => ['default' => FALSE],
  66. 'items_per_page_label' => ['default' => $this->t('Items per page')],
  67. 'items_per_page_options' => ['default' => '5, 10, 25, 50'],
  68. 'items_per_page_options_all' => ['default' => FALSE],
  69. 'items_per_page_options_all_label' => ['default' => $this->t('- All -')],
  70. 'offset' => ['default' => FALSE],
  71. 'offset_label' => ['default' => $this->t('Offset')],
  72. ],
  73. ];
  74. $options['tags'] = [
  75. 'contains' => [
  76. 'previous' => ['default' => $this->t('‹ Previous')],
  77. 'next' => ['default' => $this->t('Next ›')],
  78. ],
  79. ];
  80. return $options;
  81. }
  82. /**
  83. * Provide the default form for setting options.
  84. */
  85. public function buildOptionsForm(&$form, FormStateInterface $form_state) {
  86. parent::buildOptionsForm($form, $form_state);
  87. $pager_text = $this->displayHandler->getPagerText();
  88. $form['items_per_page'] = [
  89. '#title' => $pager_text['items per page title'],
  90. '#type' => 'number',
  91. '#min' => 0,
  92. '#description' => $pager_text['items per page description'],
  93. '#default_value' => $this->options['items_per_page'],
  94. ];
  95. $form['offset'] = [
  96. '#type' => 'number',
  97. '#min' => 0,
  98. '#title' => $this->t('Offset (number of items to skip)'),
  99. '#description' => $this->t('For example, set this to 3 and the first 3 items will not be displayed.'),
  100. '#default_value' => $this->options['offset'],
  101. ];
  102. $form['id'] = [
  103. '#type' => 'number',
  104. '#min' => 0,
  105. '#title' => $this->t('Pager ID'),
  106. '#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."),
  107. '#default_value' => $this->options['id'],
  108. ];
  109. $form['total_pages'] = [
  110. '#type' => 'number',
  111. '#min' => 0,
  112. '#title' => $this->t('Number of pages'),
  113. '#description' => $this->t('Leave empty to show all pages.'),
  114. '#default_value' => $this->options['total_pages'],
  115. ];
  116. $form['tags'] = [
  117. '#type' => 'details',
  118. '#open' => TRUE,
  119. '#tree' => TRUE,
  120. '#title' => $this->t('Pager link labels'),
  121. '#input' => TRUE,
  122. ];
  123. $form['tags']['previous'] = [
  124. '#type' => 'textfield',
  125. '#title' => $this->t('Previous page link text'),
  126. '#default_value' => $this->options['tags']['previous'],
  127. ];
  128. $form['tags']['next'] = [
  129. '#type' => 'textfield',
  130. '#title' => $this->t('Next page link text'),
  131. '#default_value' => $this->options['tags']['next'],
  132. ];
  133. $form['expose'] = [
  134. '#type' => 'details',
  135. '#open' => TRUE,
  136. '#tree' => TRUE,
  137. '#title' => $this->t('Exposed options'),
  138. '#input' => TRUE,
  139. '#description' => $this->t('Allow user to control selected display options for this view.'),
  140. ];
  141. $form['expose']['items_per_page'] = [
  142. '#type' => 'checkbox',
  143. '#title' => $this->t('Allow user to control the number of items displayed in this view'),
  144. '#default_value' => $this->options['expose']['items_per_page'],
  145. ];
  146. $form['expose']['items_per_page_label'] = [
  147. '#type' => 'textfield',
  148. '#title' => $this->t('Items per page label'),
  149. '#required' => TRUE,
  150. '#default_value' => $this->options['expose']['items_per_page_label'],
  151. '#states' => [
  152. 'invisible' => [
  153. 'input[name="pager_options[expose][items_per_page]"]' => ['checked' => FALSE],
  154. ],
  155. ],
  156. ];
  157. $form['expose']['items_per_page_options'] = [
  158. '#type' => 'textfield',
  159. '#title' => $this->t('Exposed items per page options'),
  160. '#required' => TRUE,
  161. '#description' => $this->t('Set between which values the user can choose when determining the items per page. Separated by comma.'),
  162. '#default_value' => $this->options['expose']['items_per_page_options'],
  163. '#states' => [
  164. 'invisible' => [
  165. 'input[name="pager_options[expose][items_per_page]"]' => ['checked' => FALSE],
  166. ],
  167. ],
  168. ];
  169. $form['expose']['items_per_page_options_all'] = [
  170. '#type' => 'checkbox',
  171. '#title' => $this->t('Allow user to display all items'),
  172. '#default_value' => $this->options['expose']['items_per_page_options_all'],
  173. ];
  174. $form['expose']['items_per_page_options_all_label'] = [
  175. '#type' => 'textfield',
  176. '#title' => $this->t('All items label'),
  177. '#default_value' => $this->options['expose']['items_per_page_options_all_label'],
  178. '#states' => [
  179. 'invisible' => [
  180. 'input[name="pager_options[expose][items_per_page_options_all]"]' => ['checked' => FALSE],
  181. ],
  182. ],
  183. ];
  184. $form['expose']['offset'] = [
  185. '#type' => 'checkbox',
  186. '#title' => $this->t('Allow user to specify number of items skipped from beginning of this view.'),
  187. '#default_value' => $this->options['expose']['offset'],
  188. ];
  189. $form['expose']['offset_label'] = [
  190. '#type' => 'textfield',
  191. '#title' => $this->t('Offset label'),
  192. '#required' => TRUE,
  193. '#default_value' => $this->options['expose']['offset_label'],
  194. '#states' => [
  195. 'invisible' => [
  196. 'input[name="pager_options[expose][offset]"]' => ['checked' => FALSE],
  197. ],
  198. ],
  199. ];
  200. }
  201. public function validateOptionsForm(&$form, FormStateInterface $form_state) {
  202. // Only accept integer values.
  203. $error = FALSE;
  204. $exposed_options = $form_state->getValue(['pager_options', 'expose', 'items_per_page_options']);
  205. if (strpos($exposed_options, '.') !== FALSE) {
  206. $error = TRUE;
  207. }
  208. $options = explode(',', $exposed_options);
  209. if (!$error && is_array($options)) {
  210. foreach ($options as $option) {
  211. if (!is_numeric($option) || intval($option) == 0) {
  212. $error = TRUE;
  213. }
  214. }
  215. }
  216. else {
  217. $error = TRUE;
  218. }
  219. if ($error) {
  220. $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'));
  221. }
  222. // Make sure that the items_per_page is part of the expose settings.
  223. if (!$form_state->isValueEmpty(['pager_options', 'expose', 'items_per_page']) && !$form_state->isValueEmpty(['pager_options', 'items_per_page'])) {
  224. $items_per_page = $form_state->getValue(['pager_options', 'items_per_page']);
  225. if (array_search($items_per_page, $options) === FALSE) {
  226. $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).",
  227. ['@items_per_page' => $items_per_page])
  228. );
  229. }
  230. }
  231. }
  232. public function query() {
  233. if ($this->itemsPerPageExposed()) {
  234. $query = $this->view->getRequest()->query;
  235. $items_per_page = $query->get('items_per_page');
  236. if ($items_per_page > 0) {
  237. $this->options['items_per_page'] = $items_per_page;
  238. }
  239. elseif ($items_per_page == 'All' && $this->options['expose']['items_per_page_options_all']) {
  240. $this->options['items_per_page'] = 0;
  241. }
  242. }
  243. if ($this->isOffsetExposed()) {
  244. $query = $this->view->getRequest()->query;
  245. $offset = $query->get('offset');
  246. if (isset($offset) && $offset >= 0) {
  247. $this->options['offset'] = $offset;
  248. }
  249. }
  250. $limit = $this->options['items_per_page'];
  251. $offset = $this->current_page * $this->options['items_per_page'] + $this->options['offset'];
  252. if (!empty($this->options['total_pages'])) {
  253. if ($this->current_page >= $this->options['total_pages']) {
  254. $limit = $this->options['items_per_page'];
  255. $offset = $this->options['total_pages'] * $this->options['items_per_page'];
  256. }
  257. }
  258. $this->view->query->setLimit($limit);
  259. $this->view->query->setOffset($offset);
  260. }
  261. /**
  262. * Set the current page.
  263. *
  264. * @param $number
  265. * If provided, the page number will be set to this. If NOT provided,
  266. * the page number will be set from the pager manager service.
  267. */
  268. public function setCurrentPage($number = NULL) {
  269. if (isset($number)) {
  270. $this->current_page = max(0, $number);
  271. return;
  272. }
  273. $this->current_page = max(0, $this->pagerParameters->findPage($this->options['id']));
  274. }
  275. public function getPagerTotal() {
  276. if ($items_per_page = intval($this->getItemsPerPage())) {
  277. return ceil($this->total_items / $items_per_page);
  278. }
  279. else {
  280. return 1;
  281. }
  282. }
  283. /**
  284. * Update global paging info.
  285. *
  286. * This is called after the count query has been run to set the total
  287. * items available and to update the current page if the requested
  288. * page is out of range.
  289. */
  290. public function updatePageInfo() {
  291. if (!empty($this->options['total_pages'])) {
  292. if (($this->options['total_pages'] * $this->options['items_per_page']) < $this->total_items) {
  293. $this->total_items = $this->options['total_pages'] * $this->options['items_per_page'];
  294. }
  295. }
  296. // Don't set pager settings for items per page = 0.
  297. $items_per_page = $this->getItemsPerPage();
  298. if (!empty($items_per_page)) {
  299. $pager = $this->pagerManager->createPager($this->getTotalItems(), $this->options['items_per_page'], $this->options['id']);
  300. // See if the requested page was within range:
  301. if ($this->getCurrentPage() >= $pager->getTotalPages()) {
  302. $this->setCurrentPage($pager->getTotalPages() - 1);
  303. }
  304. }
  305. }
  306. public function usesExposed() {
  307. return $this->itemsPerPageExposed() || $this->isOffsetExposed();
  308. }
  309. protected function itemsPerPageExposed() {
  310. return !empty($this->options['expose']['items_per_page']);
  311. }
  312. protected function isOffsetExposed() {
  313. return !empty($this->options['expose']['offset']);
  314. }
  315. public function exposedFormAlter(&$form, FormStateInterface $form_state) {
  316. if ($this->itemsPerPageExposed()) {
  317. $options = explode(',', $this->options['expose']['items_per_page_options']);
  318. $sanitized_options = [];
  319. if (is_array($options)) {
  320. foreach ($options as $option) {
  321. $sanitized_options[intval($option)] = intval($option);
  322. }
  323. if (!empty($this->options['expose']['items_per_page_options_all']) && !empty($this->options['expose']['items_per_page_options_all_label'])) {
  324. $sanitized_options['All'] = $this->options['expose']['items_per_page_options_all_label'];
  325. }
  326. $form['items_per_page'] = [
  327. '#type' => 'select',
  328. '#title' => $this->options['expose']['items_per_page_label'],
  329. '#options' => $sanitized_options,
  330. '#default_value' => $this->getItemsPerPage(),
  331. ];
  332. }
  333. }
  334. if ($this->isOffsetExposed()) {
  335. $form['offset'] = [
  336. '#type' => 'textfield',
  337. '#size' => 10,
  338. '#maxlength' => 10,
  339. '#title' => $this->options['expose']['offset_label'],
  340. '#default_value' => $this->getOffset(),
  341. ];
  342. }
  343. }
  344. public function exposedFormValidate(&$form, FormStateInterface $form_state) {
  345. if (!$form_state->isValueEmpty('offset') && trim($form_state->getValue('offset'))) {
  346. if (!is_numeric($form_state->getValue('offset')) || $form_state->getValue('offset') < 0) {
  347. $form_state->setErrorByName('offset', $this->t('Offset must be a number greater than or equal to 0.'));
  348. }
  349. }
  350. }
  351. /**
  352. * {@inheritdoc}
  353. */
  354. public function getCacheMaxAge() {
  355. return Cache::PERMANENT;
  356. }
  357. /**
  358. * {@inheritdoc}
  359. */
  360. public function getCacheContexts() {
  361. // The rendered link needs to play well with any other query parameter used
  362. // on the page, like other pagers and exposed filter.
  363. return ['url.query_args'];
  364. }
  365. /**
  366. * {@inheritdoc}
  367. */
  368. public function getCacheTags() {
  369. return [];
  370. }
  371. }