Version 1
[yaffs-website] / web / core / modules / views / src / Plugin / views / pager / Some.php
1 <?php
2
3 namespace Drupal\views\Plugin\views\pager;
4
5 use Drupal\Core\Form\FormStateInterface;
6
7 /**
8  * Plugin for views without pagers.
9  *
10  * @ingroup views_pager_plugins
11  *
12  * @ViewsPager(
13  *   id = "some",
14  *   title = @Translation("Display a specified number of items"),
15  *   help = @Translation("Display a limited number items that this view might find."),
16  *   display_types = {"basic"}
17  * )
18  */
19 class Some extends PagerPluginBase {
20
21   public function summaryTitle() {
22     if (!empty($this->options['offset'])) {
23       return $this->formatPlural($this->options['items_per_page'], '@count item, skip @skip', '@count items, skip @skip', ['@count' => $this->options['items_per_page'], '@skip' => $this->options['offset']]);
24     }
25     return $this->formatPlural($this->options['items_per_page'], '@count item', '@count items', ['@count' => $this->options['items_per_page']]);
26   }
27
28   protected function defineOptions() {
29     $options = parent::defineOptions();
30     $options['items_per_page'] = ['default' => 10];
31     $options['offset'] = ['default' => 0];
32
33     return $options;
34   }
35
36   /**
37    * Provide the default form for setting options.
38    */
39   public function buildOptionsForm(&$form, FormStateInterface $form_state) {
40     parent::buildOptionsForm($form, $form_state);
41     $pager_text = $this->displayHandler->getPagerText();
42     $form['items_per_page'] = [
43       '#title' => $pager_text['items per page title'],
44       '#type' => 'textfield',
45       '#description' => $pager_text['items per page description'],
46       '#default_value' => $this->options['items_per_page'],
47     ];
48
49     $form['offset'] = [
50       '#type' => 'textfield',
51       '#title' => $this->t('Offset (number of items to skip)'),
52       '#description' => $this->t('For example, set this to 3 and the first 3 items will not be displayed.'),
53       '#default_value' => $this->options['offset'],
54     ];
55   }
56
57   public function usePager() {
58     return FALSE;
59   }
60
61   public function useCountQuery() {
62     return FALSE;
63   }
64
65   public function query() {
66     $this->view->query->setLimit($this->options['items_per_page']);
67     $this->view->query->setOffset($this->options['offset']);
68   }
69
70 }