Drupal 8 & Drupal 9 - Views - créer un Pager personnalisé

Posté le Jeudi 8 octobre 2020 - 07:30

Pour un projet j'avais besoin d'afficher une pagination particulière sur une vue.

La première page devait afficher 9 éléments, et les suivantes 12.

J'ai donc pour cela créé un Pager personnalisé pour Views, et ajouté ce paramètre en option.

En voici le code, disponible aussi sur github : https://github.com/kgaut/kgaut_tools/blob/2.0.x/src/Plugin/views/pager/…

Ce code est à placer dans un module, au chemin suivant : modules/custom/mon_module/src/Plugin/views/pager/PagerFullWithSpecificFirstPage.php

  1. <?php
  2.  
  3. namespace Drupal\mon_module\Plugin\views\pager;
  4.  
  5. use Drupal\Core\Form\FormStateInterface;
  6. use Drupal\views\Plugin\views\pager\Full;
  7.  
  8. /**
  9.  * The plugin to handle full pager.
  10.  *
  11.  * @ingroup views_pager_plugins
  12.  *
  13.  * @ViewsPager(
  14.  * id = "full_with_first_page",
  15.  * title = @Translation("Paged output, full pager with a specific number of items for the first page"),
  16.  * short_title = @Translation("Pager Full - specific first page"),
  17.  * help = @Translation("Pager full with a specific number of items for the first page"),
  18.  * theme = "pager",
  19.  * register_theme = FALSE
  20.  * )
  21.  */
  22. class PagerFullWithSpecificFirstPage extends Full {
  23.  
  24. /**
  25.   * {@inheritdoc}
  26.   */
  27. protected function defineOptions() {
  28. $options = parent::defineOptions();
  29. $options['items_per_page_first_page'] = ['default' => 10];
  30. unset($options['expose']);
  31. return $options;
  32. }
  33.  
  34. /**
  35.   * {@inheritdoc}
  36.   */
  37. public function buildOptionsForm(&$form, FormStateInterface $form_state) {
  38. parent::buildOptionsForm($form, $form_state);
  39. $pager_text = $this->displayHandler->getPagerText();
  40. $form['items_per_page']['#weight'] = -2;
  41. $form['items_per_page_first_page'] = [
  42. '#title' => $pager_text['items per page title'] . ' for the first page',
  43. '#type' => 'number',
  44. '#min' => 0,
  45. '#weight' => -1,
  46. '#description' => $pager_text['items per page description'],
  47. '#default_value' => $this->options['items_per_page_first_page'],
  48. ];
  49.  
  50. unset($form['expose']);
  51. }
  52.  
  53. /**
  54.   * {@inheritdoc}
  55.   */
  56. public function validateOptionsForm(&$form, FormStateInterface $form_state) {
  57. // Only accept integer values.
  58. $error = FALSE;
  59. }
  60.  
  61. /**
  62.   * {@inheritdoc}
  63.   */
  64. public function summaryTitle() {
  65. if (!empty($this->options['offset'])) {
  66. if ($this->options['items_per_page'] !== $this->options['items_per_page_first_page']) {
  67. return $this->formatPlural($this->options['items_per_page'], '@count item (@count_first for the first page), skip @skip', 'Paged, @count items, skip @skip', [
  68. '@count' => $this->options['items_per_page'],
  69. '@count_first' => $this->options['items_per_page_first_page'],
  70. '@skip' => $this->options['offset'],
  71. ]);
  72. }
  73. return $this->formatPlural($this->options['items_per_page'], '@count item, skip @skip', 'Paged, @count items, skip @skip', [
  74. '@count' => $this->options['items_per_page'],
  75. '@skip' => $this->options['offset'],
  76. ]);
  77. }
  78. if ($this->options['items_per_page'] !== $this->options['items_per_page_first_page']) {
  79. return $this->formatPlural($this->options['items_per_page'], '@count item', 'Paged, @count items (@count_first for the first page)', [
  80. '@count' => $this->options['items_per_page'],
  81. '@count_first' => $this->options['items_per_page_first_page'],
  82. ]);
  83. }
  84. return $this->formatPlural($this->options['items_per_page'], '@count item', 'Paged, @count items', ['@count' => $this->options['items_per_page']]);
  85. }
  86.  
  87. /**
  88.   * {@inheritdoc}
  89.   */
  90. public function query() {
  91. if ($this->current_page === 0) {
  92. $this->options['items_per_page'] = $this->options['items_per_page_first_page'];
  93. }
  94. $limit = $this->options['items_per_page'];
  95. $offset = $this->options['offset'];
  96. if ($this->current_page > 0) {
  97. $offset += ($this->current_page - 1) * $this->options['items_per_page'];
  98. $offset += $this->options['items_per_page_first_page'];
  99. }
  100. if (!empty($this->options['total_pages'])) {
  101. if ($this->current_page >= $this->options['total_pages']) {
  102. $limit = $this->options['items_per_page'];
  103. $offset = $this->options['total_pages'] * $this->options['items_per_page'];
  104. }
  105. }
  106. $this->view->query->setLimit($limit);
  107. $this->view->query->setOffset($offset);
  108. }
  109.  
  110. public function updatePageInfo() {
  111. if (!empty($this->options['total_pages'])) {
  112. if (($this->options['total_pages'] * $this->options['items_per_page']) < $this->total_items) {
  113. $this->total_items = $this->options['total_pages'] * $this->options['items_per_page'];
  114. }
  115. }
  116.  
  117. // Don't set pager settings for items per page = 0.
  118. $items_per_page = $this->getItemsPerPage();
  119. $items_per_page_first = $this->getItemsPerPageFirst();
  120. if (!empty($items_per_page)) {
  121. // quick fix if specific number on first page
  122. $total_items = (int) $this->getCurrentPage() !== 0 ? $this->getTotalItems() + $items_per_page_first - 1 : $this->getTotalItems();
  123. $pager = $this->pagerManager->createPager($total_items, $this->options['items_per_page'], $this->options['id']);
  124. // See if the requested page was within range:
  125. if ($this->getCurrentPage() >= $pager->getTotalPages()) {
  126. $this->setCurrentPage($pager->getTotalPages() - 1);
  127. }
  128. }
  129. }
  130.  
  131. public function getItemsPerPageFirst() {
  132. return isset($this->options['items_per_page_first_page']) ? $this->options['items_per_page_first_page'] : 0;
  133. }
  134.  
  135. }

Vous pouvez retrouver

Aller plus loin ?

 

Ajouter un commentaire

Ne sera pas publié

HTML restreint

  • Balises HTML autorisées : <a href hreflang> <em> <strong> <cite> <blockquote cite> <code> <ul type> <ol start type> <li> <dl> <dt> <dd> <h2 id> <h3 id> <h4 id> <h5 id> <h6 id>
  • Les lignes et les paragraphes vont à la ligne automatiquement.
  • Les adresses de pages web et les adresses courriel se transforment en liens automatiquement.
CAPTCHA Désolé, pour ça, mais c'est le seul moyen pour éviter le spam...